Closed Bug 1779974 Opened 2 years ago Closed 2 years ago

Javascript division provides strange results

Categories

(Firefox :: Untriaged, defect)

Firefox 104
defect

Tracking

()

RESOLVED DUPLICATE of bug 5856

People

(Reporter: srinivasr2007, Unassigned)

Details

User Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Steps to reproduce:

I am building a metrics converter website that performs intensive calculations and i am mainly using JS to perform all these calculations.

There are many scenarios where I get decimal numbers with different precision(much longer), I can provide a few instances here.

Actual results:

Eg - Centimeters to Meters
So let's say if a user enters 0.009 centimeters and the formula to get the meters would be to divide the centimeter value by 100

0.009/100
Provides - 0.00008999999999999999

Expected results:

The Expected result should be just 0.00009, this works perfectly in other languages like python, c# etc.

0.009/100
Provides - 0.00009

Few More instances to be noted

0.007/100 results in 0.00007000000000000001
0.00009/100 results in 9.000000000000001e-7

The same can be replicated on the site I have mentioned by providing the value in the centimeter https://www.metricsconverter.com/length/centimeters-to-meters/

I am sure there would be many other floating numbers that will cause this issue and it will be a bad experience for the user. Is there anyway to solve it or any workaround without using the toFixed() method?

Status: UNCONFIRMED → RESOLVED
Closed: 2 years ago
Resolution: --- → DUPLICATE

I believe i need to use a parseFloat function with toFixed() which solves the issue which I got from Stackoverflow https://stackoverflow.com/questions/73019564/javascript-division-on-floating-returns-strange-result

`parseFloat((0.009/100).toFixed(10))`
You need to log in before you can comment on or make changes to this bug.