rounding number to a given decimal length

Questions, tips and tricks and techniques for scripting in Servoy

rounding number to a given decimal length

Postby faheemhameed » Sun Dec 12, 2004 2:13 pm

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
Hameed
Pilot simple software
Hong Kong
User avatar
faheemhameed
 
Posts: 763
Joined: Wed Sep 10, 2003 7:23 am

Postby ROCLASI » Sun Dec 12, 2004 4:41 pm

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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Postby automazione » Mon Dec 13, 2004 12:17 am

Perhaps this thread may help:

http://forum.servoy.com/viewtopic.php?t=2034
Enrico Arata
Servoy Italia
automazione
 
Posts: 366
Joined: Thu Apr 24, 2003 11:37 am
Location: Torino, Italy

Postby Harjo » Wed Dec 27, 2006 9:44 pm

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.)
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Postby tweetie » Thu Dec 28, 2006 10:38 am

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
User avatar
tweetie
 
Posts: 345
Joined: Thu Jan 08, 2004 11:32 am
Location: Rotterdam, Netherlands

Postby SteveInLA » Fri Dec 29, 2006 12:56 am

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
SteveInLA
 
Posts: 233
Joined: Thu Jul 29, 2004 12:00 am
Location: Southern Oregon, USA

Postby Harjo » Fri Dec 29, 2006 1:38 am

Thanks Tweetie!

this
Code: Select all
Math.round(10.455*100)/100
works great for me in calculations!
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Postby Riccardino » Fri Dec 29, 2006 10:31 am

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?
ciao, ric
User avatar
Riccardino
 
Posts: 911
Joined: Thu Apr 24, 2003 11:42 am
Location: Ferrara, Italy

Postby Harjo » Fri Dec 29, 2006 1:07 pm

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.
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

Postby Riccardino » Fri Dec 29, 2006 2:59 pm

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))
ciao, ric
User avatar
Riccardino
 
Posts: 911
Joined: Thu Apr 24, 2003 11:42 am
Location: Ferrara, Italy

Postby jcompagner » Mon Jan 15, 2007 4:12 pm

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!
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8829
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Postby Harjo » Mon Jan 15, 2007 4:38 pm

that's why I'm using this now: for exact storage of numbers
Code: Select all
Math.round(10.455*100)/100
;-)
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands


Return to Methods

Who is online

Users browsing this forum: No registered users and 10 guests

cron