|
-
Jan 23rd, 2004, 10:28 AM
#1
Thread Starter
Hyperactive Member
JavaScript: Addding floats & formatting nicely...
I have a form in HTML that has several text boxes where the users enter numbers.
I have a textbox that is to be the sum of those numbers.
I have javascript which parses the values entered by the user to floats, adds them, and displays them in the final text box.
The problem is that if the users enter certain decimal values, I get odd answers.
For example:
Text1 = 10
Text2 = 5
Text3 = 20.52
TotalText = 35.519999999999996
How can I make TotalText show 35.52?
Thanks!
-
Jan 23rd, 2004, 11:09 AM
#2
Frenzied Member
Math.round(number) will round any value off to the nearest whole number. If you want it to round to two decimal places do:
number = number*100
number = Math.round(number)
number = number/100
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|