rounding number to a given decimal length

How can I round a number to two decimal points.

For example the following code returns the value 10:

Math.round(10.455)

I want round this to a two decimal point number. I suppose the return the value of 10.46

Thanks

var myNum = 10.455;
myNum = myNum.toFixed(2);

This should do it.
Although for me it returns 10.456 (!).
Changing the input-number to 10.4551 makes it work correctly.
Seems like a bug to me.

Perhaps this thread may help:

http://forum.servoy.com/viewtopic.php?t=2034

ROCLASI:

var myNum = 10.455;

myNum = myNum.toFixed(2);




This should do it.
Although for me it returns 10.456 (!).
Changing the input-number to 10.4551 makes it work correctly.
Seems like a bug to me.

I’m strugling with this one also!

When I do this:

return parseFloat((19 /100 * 487.50).toFixed(2));

it is returning: 92,623
(this is weird, because I set the toFixed to two)

When I do this:

return (19 /100) * 487.50

it is returning: 92,625

When I use now a format on that field, it shows indeed: 92,63

In my backend column I want also return 92,63
Because if we don’t we have rounding problems with larger aggregates.

I followed all the links regarding ‘rounding’ and toFixed, but none of them has an answer to this question.

so bottomline, is this a bug, or am I doing something wrong? (I think I am! ;-) )
Can this be fixed,/changed by method, instead of changing columns from double to numeric?? (as suggested by David, somewhere else on the forum.)

Math.round(10.455*100)/100

or

//format a number to specification (or to have a defined fraction)
var textalNumber = utils.numberFormat(16.749, 2); //returns 16.75
var textalNumber2 = utils.numberFormat(100006.749, '#,###.00'); //returns 100,006.75

As stated by Jan Blok in the post referenced by Enrico, the toFixed method returns a string. Likewise, utils.numberFormat also returns a string. Here is a global method I use:

var numToRound		= arguments[ 0 ];
var decimalPlaces	= arguments[ 1 ];

var multiplier	 = Math.pow( 10, decimalPlaces );
return Math.round (multiplier * numToRound ) / multiplier;

Pass it your number and an integer value representing the number of decimal places to round to. Viola!

Steve In LA

Thanks Tweetie!

this

Math.round(10.455*100)/100
``` works great for me in calculations!

SteveInLA:
As stated by Jan Blok in the post referenced by Enrico, the toFixed method returns a string

But using parseFloat shouldn’t convert this string to a number?

that’s true, but the toFixed function does weird stuff something.

(or I’m using it wrong. so I recommend for currency NOT to use the parseFloat((19 /100 * 487.50).toFixed(2)) function.

HJK:
that’s true, but the toFixed function does weird stuff something.

(or I’m using it wrong. so I recommend for currency NOT to use the parseFloat((19 /100 * 487.50).toFixed(2)) function.

Just for info:
what happens if you place all the calcs in a var and then use is with parseFloat and toFixed?
e.g.
var theAmount = 19 /100 * 487.50
parseFloat((theAmount).toFixed(2))

this:

var myNum = 10.455;

myNum = myNum.toFixed(2);




This should do it.
Although for me it returns 10.456 (!).
Changing the input-number to 10.4551 makes it work correctly.
Seems like a bug to me.

that seems really a bug in the script engine. Because the browsers (IE en FF) both seems to return the right value: 10.46. I will look if this is also still a bug in the latest script engine.

Do remember that you can’t round it to a value like 10.46 and then parse it again as a float! because that will cause it again to be a floating point (IEEE notation) number. So after the rounding you can have 10.46 (as a string) and then if you parseFloat it again it can be come 10.460000009 or something!

that’s why I’m using this now: for exact storage of numbers

Math.round(10.455*100)/100
``` <img src="{SMILIES_PATH}/icon_wink.gif" alt=";-)" title="Wink" />