PDA

View Full Version : Rounding Numbers


angst
01-05-2006, 04:43 PM
hello,

I've built this little function for rounding numbers, money totals in particular.


function roundme(number) {
var result=Math.round(number*100)/100
return result;
}


the problem is that is seems to always around to only one decimal place,
like $1960.4

so, my question is, how can I make sure that it always comes out as two decimal places like it should when calculating money?

thanks in advance for your time!
-Ken

Nischumacher
01-05-2006, 05:03 PM
var result=(Math.round(number*100)/100).toFixed(2);

angst
01-05-2006, 05:10 PM
hmm,
no, that completly broke the script, it doesn't work at all.

any other ideas?

thanks again!
-ken

fishluvr
01-05-2006, 06:21 PM
The toFixed(2) should be applied after you're done with any calculations as it returns a string vice a number...

mark87
01-05-2006, 06:40 PM
function roundme(number) {
var result=Math.round(number*100)/100;
result=result.toFixed(2);
return result;
}

angst
01-05-2006, 07:28 PM
kool,
that works!

thanks guys:thumbsup:

-Ken

requestcode
01-05-2006, 07:58 PM
Or this:
function roundme(number) {
return result=(Math.round(number*100)/100).toFixed(2);
}

Same thing just reduced to one line