[thelist] JS: Rounding floats

Frank frank at frankmarion.com
Mon Dec 8 01:26:48 CST 2003

I'm trying to "dollar format" some calculated floats. I'm currently using this

// Rounds number to x decimal places, defaults to 2
function round_num(number,x) {
         x = (!x ? 2 : x);
         return Math.round(number*Math.pow(10,x))/Math.pow(10,x);

The downside is that if the calculation turns out to end in a zero, the 
zero is omitted. Thus

5.00 = 5
1.50 = 1.5

Does anyone have a very tight and concise manner of either padding for the 
dollar-format display?

Frank Marion <frank at frankmarion.com>  

More information about the thelist mailing list