Page 1 of 1

rounding number to a given decimal length

PostPosted: Sun Dec 12, 2004 2:13 pm
by faheemhameed
How can I round a number to two decimal points.

For example the following code returns the value 10:
Code: Select all
Math.round(10.455)


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

Thanks

PostPosted: Sun Dec 12, 2004 4:41 pm
by ROCLASI
Code: Select all
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.

PostPosted: Mon Dec 13, 2004 12:17 am
by automazione
Perhaps this thread may help:

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

PostPosted: Wed Dec 27, 2006 9:44 pm
by Harjo
ROCLASI wrote:
Code: Select all
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:
Code: Select all
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:

Code: Select all
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.)

PostPosted: Thu Dec 28, 2006 10:38 am
by tweetie
Code: Select all
Math.round(10.455*100)/100

or
Code: Select all
//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

PostPosted: Fri Dec 29, 2006 12:56 am
by SteveInLA
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:
Code: Select all
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

PostPosted: Fri Dec 29, 2006 1:38 am
by Harjo
Thanks Tweetie!

this
Code: Select all
Math.round(10.455*100)/100
works great for me in calculations!

PostPosted: Fri Dec 29, 2006 10:31 am
by Riccardino
SteveInLA wrote: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?

PostPosted: Fri Dec 29, 2006 1:07 pm
by Harjo
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.

PostPosted: Fri Dec 29, 2006 2:59 pm
by Riccardino
HJK wrote: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))

PostPosted: Mon Jan 15, 2007 4:12 pm
by jcompagner
this:

Code: Select all
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!

PostPosted: Mon Jan 15, 2007 4:38 pm
by Harjo
that's why I'm using this now: for exact storage of numbers
Code: Select all
Math.round(10.455*100)/100
;-)