开发者

Using a round up JavaScript

I have this JavaScript below that add different fields. It works but does not round up the sum:

function Field2Value(aFields) {
  var aValues = new Array(aFields.length);
  for(i = 0; i < aFields.length; i++) {
    aValues[i] = this.getField(aFields[i]).value;
  }

  return aValues;
} // end Field2Value

function SumArray(aValues) {
  var sum = 0;
  for(i = 0; i < aValues.length; i++) {
    if(isNaN(aValues[i])) aValues[i] = 0;
    sum += Number(aValues[i]);
  }

  return sum;
} // end SumArray

var myFields = new Array("AverageDiv", "AverageDiv1", "AverageDiv2",
  "AverageDiv3", "AverageDiv4", "AverageDiv5", "AverageDiv6", "Avera开发者_StackOverflow中文版geDiv7",
  "AverageDiv8", "AverageDiv9", "AverageDiv10", "AverageDiv11",
  "AverageDiv12", "AverageDiv13");

event.value = SumArray( Field2Value(myFields) );

function Round(nValue, nDec) {
  return Number(util.printf( "%,0 ." + Number(nDec) + "f", Number(nValue))) ;
}

I also have a script I want to use that will round up the sum of the average dividends but I do not know where to place this script in the script above.

Here is the rounding script:

function Round(nValue, nDec) {
  return Number(util.printf( "%,0 ." + Number(nDec) + "f", Number(nValue))) ;
}

Please help.


The easiest way to round a number using Javascript is to use Math.round(): http://www.w3schools.com/jsref/jsref_round.asp

Edit - looking more closely, it looks like you're round to a set number of decimal places. You're using 'util.printf' - is that a library call? Does that function exist? That could be why your function is failing.

Put your code in a code block and it would be a lot easier to read.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜