0.07*0.03 = 0.0021000000000000003

User Help for Mozilla Firefox
Post Reply
jslomp
New Member
Posts: 1
Joined: September 5th, 2021, 9:08 am

0.07*0.03 = 0.0021000000000000003

Post by jslomp »

Hello Firefox dev team.

This bug is very old and i have noticed this with javascript development but i was young and believed that i did something wrong. Now in my invoice calculator i did amount * price. 0.07 * 0.03 and it returned 0.0021000000000000003

i have seen this behavior before and always rounded it, but since i am older i realize this is just a very old bug that no-one ever tended to fix.
if you go and use your calculator, the correct answer is of course: 0.0021.

The weird part is, Chrome/Edge and Internet explorer all return the same (wrong) answer.
User avatar
dickvl
Posts: 54161
Joined: July 18th, 2005, 3:25 am

Re: 0.07*0.03 = 0.0021000000000000003

Post by dickvl »

This is caused by the difference of how fixed point numbers and floating point numbers are stored and 7 is a problem: 0.0007*1e2 -> 0.06999999999999999, so you may need to round numbers.

If you want to work accurately with fragmented numbers then use integers and shift the decimal point by multiplying with 100 or 1000.
Post Reply