Search code examples
javascriptgoogle-chromegoogle-chrome-devtoolsv8exponent

Chrome - Javascript: Negative Exponent Calculation is wrong for > -3


I am working on React project.

I have use case where I have to add step attribute on Number <Input> tag. This step attribute will be dynamic. Based on array data.

Array data is [{ precision: 2 }, { precision: 4 }, { precision: 5 }].

So I generate the step like:

<input type="number" step={ 10 ** -precision } >

Here 10 ** -precision will output :

0.01 for precision = 2

0.0001 for precision = 4

0.00001 fror precision = 5

This is what I want, and it is working fine in Firefox and Opera.

But Chrome is the issue, Chrome's output for 10 ** -4 is 0.00009999999999999999. Which is breaking in my case.

Chrome's output is correct till 10 ** -3 after that it is printing wrong.

enter image description here

My chrome version is : 74.0.3729.131 and OS: MacOs Mojave: 10.14.4


Solution

  • Maybe it's a floating point precision issue. If you only have to pass it as prop to the tag you can try <input type="number" step={ (10 ** -precision).toFixed(precision) } />