# Thread: Question on rounding off a sum

1. ## Question on rounding off a sum

Hi. I have this JavaScript function that displays the sum of all the values of a column. Is there any way that I can modify the code to round the sum off to two decimal points?

Code:
function calculateFooterValues(stage){
if(stage && stage!=2)
return true;
var srQ = document.getElementById("sr_q");
srQ.innerHTML = "\$"+sumColumn(6)
return true;
}
function sumColumn(ind){
var out = 0;
for(var i=0;i<mygrid.getRowsNum();i++){
out+= parseFloat(mygrid.cells2(i,ind).getValue())
}
return out;

2. You need Math.round. You'll need to apply that to out before returning it.

3. Originally Posted by Raffles
You need Math.round. You'll need to apply that to out before returning it.
Thanks for replying. Is there a way to have math.round round off two places after the decimal point? For example, the result I get for out is

\$34376.49999999998

and I'd like the result to be

\$34376.50

4. Thanks again! I think I'm getting close. I've modified the code to this:

Code:
function sumColumn(ind){
var out = 0;
for(var i=0;i<mygrid.getRowsNum();i++){
out+= parseFloat(mygrid.cells2(i,ind).getValue())
}
return Math.round(out * 100.00) / 100 ;out;
}
Now I'm getting this value for out:

\$34376.5

\$34376.50

5. Use toFixed

6. Originally Posted by ScallioXTX
Use toFixed
U TH' MAN! This worked:

Code:
function sumColumn(ind){
var out = 0;
for(var i=0;i<mygrid.getRowsNum();i++){
out+= parseFloat(mygrid.cells2(i,ind).getValue())
}
return out.toFixed(2) ;out;
}

7. Note that .toFixed doesn't round. So 1.059 will be returned as 1.05 (should be 1.06).

If you also want rounding you should probably do Math.round(number).toFixed(2)

No, it does round. But please check a view inputs to see if you get what you want. The back of my mind is telling me there's something odd about .toFixed() ...

8. Originally Posted by ScallioXTX
Note that .toFixed doesn't round. So 1.059 will be returned as 1.05 (should be 1.06).

If you also want rounding you should probably do Math.round(number).toFixed(2)

No, it does round. But please check a view inputs to see if you get what you want. The back of my mind is telling me there's something odd about .toFixed() ...
OK. Someone at another forum suggested this:

Code:
function sumColumn(ind){
var out = 0;
for(var i=0;i<mygrid.getRowsNum();i++){
out+= parseFloat(mygrid.cells2(i,ind).getValue())
}
out = "" + (Math.round(out * 100)) / 100 ;
if (out.indexOf(".") == out.length-2) { out +='0' ;
}
return out ;
}
Seems to work. What do you think?

9. Yup looks fine

10. It may work but have you validated the code?

http://www.jslint.com/

The following validates:
Code JavaScript:
function sumColumn(ind) {
var out = 0, i, mygrid;

for (i = 0; i < mygrid.getRowsNum(); i += 1) {
out += parseFloat(mygrid.cells2(i, ind).getValue());
}

out = "" + (Math.round(out * 100)) / 100;

if (out.indexOf(".") === out.length - 2) {
out += "0";
}

return out;
}

11. If in doubt combine round and tofixed (that way you take care of any rounding issues with tofixed).

Code:
function sumColumn(ind) {
var out = 0, i, mygrid;

for (i = 0; i < mygrid.getRowsNum(); i += 1) {
out += parseFloat(mygrid.cells2(i, ind).getValue());
}
return ((Math.round(out * 100)) / 100).toFixed(2);
}

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•