The error is happening when I add 34.09 + 34.09 + 34.09 + 34.09 + 34.09, I get 170.455 and when I do 170.455.toFixed(2) I get 170.46 which should be 170.45 and not 170.46.
javascript does not do decimal arithmetic. all numbers are 64 bit floats. floats (because they are binary rather than decimal) can not be used for money, as you will get precision errors. this is usually covered in comp sci 101, so I assume you don't have
a formal edition in computer science.
just like 1/3 is .33333.. in decimal, several decimal number are repeating binaries. see:
the typical solution to calculating money with floats is to use a fixed precision library (like C# money) or do all the calculations in pennies (or mills if more precision required) and divide by 100 (or 1000 if mills)
toFixed rounds to the number of decimal places requested. 170.455 when
rounded to two decimal places id 170.46.
One way to truncate to two decimal places would be to multiply by 100, take the .floor (which finds the largest integer smaller than or equal to the argument) and then divide by 100. (If you want three decimal places then use 1,000 instead of 100, etc)
Member
61 Points
460 Posts
JavaScript Decimal Math
Mar 04, 2021 05:10 PM|joegreen2005|LINK
Hello,
When I do 34.09 * 11, I get 374.99 but if I add 340.91 and 34.09 in JavaScript, I get 375.00 instead of 374.99. Here is my code:
Why JavaScript is rounding 374.99 to 375. What do I need to do to get 374.99?
Joe
All-Star
53711 Points
24038 Posts
Re: JavaScript Decimal Math
Mar 04, 2021 06:42 PM|mgebhard|LINK
Your math is wrong.
Maybe you mean
Plus your code has a bug.
Member
61 Points
460 Posts
Re: JavaScript Decimal Math
Mar 04, 2021 07:27 PM|joegreen2005|LINK
The error is happening when I add 34.09 + 34.09 + 34.09 + 34.09 + 34.09, I get 170.455 and when I do 170.455.toFixed(2) I get 170.46 which should be 170.45 and not 170.46.
All-Star
58474 Points
15790 Posts
Re: JavaScript Decimal Math
Mar 04, 2021 09:22 PM|bruce (sqlwork.com)|LINK
javascript does not do decimal arithmetic. all numbers are 64 bit floats. floats (because they are binary rather than decimal) can not be used for money, as you will get precision errors. this is usually covered in comp sci 101, so I assume you don't have a formal edition in computer science.
just like 1/3 is .33333.. in decimal, several decimal number are repeating binaries. see:
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
the typical solution to calculating money with floats is to use a fixed precision library (like C# money) or do all the calculations in pennies (or mills if more precision required) and divide by 100 (or 1000 if mills)
> (3409 + 3409 + 3409 + 3409 + 3409) / 100
< 170.45
you can do a simple javascript math package:
then:
pennyMath.sum(34.09 , 34.09 , 34.09 , 34.09 , 34.09)
gives 170.45
Participant
1660 Points
952 Posts
Re: JavaScript Decimal Math
Mar 04, 2021 10:28 PM|PaulTheSmith|LINK
toFixed rounds to the number of decimal places requested. 170.455 when rounded to two decimal places id 170.46.
One way to truncate to two decimal places would be to multiply by 100, take the .floor (which finds the largest integer smaller than or equal to the argument) and then divide by 100. (If you want three decimal places then use 1,000 instead of 100, etc)
const truncated = (Math.floor(myNum * 100) / 100).toString();
(What do you want to do with negative numbers? The above will display -4.567 as -4.57)