English 中文(简体)
Java神学问题
原标题:Problem with JavaScript arithmetic
  • 时间:2010-06-02 22:13:22
  •  标签:
  • javascript

我有表格供客户增加预算预测。 知名用户希望能够以美元、基拉-美元或美加美元显示美元价值。

我试图通过一组称为 Java语功能的无线电台塔尼来实现这一目标,但是在四舍五入时却存在问题,使结果看上去看上去了血清。

任何建议都会受到高度赞赏!

Lynn

function setDollars(new_mode)
 {
  var factor;
  var myfield;
  var myval;

  var cur_mode = document.proj_form.cur_dollars.value;

  if(cur_mode == new_mode)
   {
    return;
   }
  else if((cur_mode ==  d )&&(new_mode ==  kd ))
   {
     factor = "0.001";
   }
  else if((cur_mode ==  d )&&(new_mode ==  md ))
   {
     factor = "0.000001";
   }
  else if((cur_mode ==  kd )&&(new_mode ==  d ))
   {
     factor = "1000";
   }
  else if((cur_mode ==  kd )&&(new_mode ==  md ))
   {
     factor = "0.001";
   }
  else if((cur_mode ==  md )&&(new_mode ==  kd ))
   {
     factor = "1000";
   }
  else if((cur_mode ==  md )&&(new_mode ==  d ))
   {
     factor = "1000000";
   }

  document.proj_form.cur_dollars.value = new_mode;

  var cur_idx = document.proj_form.cur_idx.value;
  var available_slots = 13 - cur_idx;
  var td_name;
  var cell;
  var new_value;

  //Adjust dollar values for projections
  for(i=1;i<13;i++)
   {
    var myfield =  eval( document.proj_form.proj_ +i);
    if(myfield.value ==   )
     {
      myfield.value = 0;
     }
    var myval = parseFloat(myfield.value) * parseFloat(factor);
    myfield.value = myval;
    if(i < cur_idx)
     {
      document.getElementById("actual_"+i).innerHTML = myval;
   }
 }
问题回答

首先,Don t <>/strong> set myfield. Value = myval after do the math. 你每多选一个无线电台,就会产生四舍五入的错误。 保留我的田地。 然后计算<>>>><>> >值。 这也将减少您的脚步中的案件数目,因为你总是将美元兑换。

现在按司计算——由于你总是朝同一方向转变,你永远不必乘以0.001或1000。

利用Math.round()控制四舍五入的错误。 首先,你可以把原来的价值乘以一个更大的因素,帮助控制四舍五入的错误。

newvalue = value / 1000;
newvalue = (value * 10) / 10000;

增 编

switch (new_mode)
{
    case  d :
        divisor = 1;
        break;
    case  kd :
        divisor = 1000;
        break;
    case  md :
        divisor = 1000000;
        break;
}
var displayValue = myfield.value / divisor;

另外,没有分配数字编号divisor = “1000”;使用实际编号divisor =1000<>code>(无引自)

Java字浮点数是不断的刺激来源。

你们应努力在Fixed()上加一,以减少不可避免的荒谬的恶魔,我实际上不得不迫使它 round倒,我不关心的是:

   number_to_be_rounded = Math.round(number_to_be_rounded*1000)/1000;
   number_to_be_rounded = parseFloat(number_to_be_rounded.toFixed(2));

很奇怪,但对于非金融体系来说,它已经足够接近。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.