s-yadav / react-number-format

React component to format numbers in an input or as a text.
MIT License
3.9k stars 410 forks source link

Rounding of numbers does not work #788

Closed Mrinalini99 closed 1 year ago

Mrinalini99 commented 1 year ago

Hi Team

The rounding off numbers does not work properly. I suspect it is because of .toFixed()

Reproducible test case <NumberFormat Value ={0.175} DecimalScale = {2}

expected is 0.18 but the given output is 0.17 But if the value is passed as 0.125 Then the expected value and output is same as 0.12.

But if the value is passed as 0.195 Then the expected value is 0.20 but the output is 0.19

Version used - 5.3.0, 5.2.2 and also 4.6.4

Please let us know the fix Thanks

s-yadav commented 1 year ago

To round, lib is using Number.toFixed() and the behaviour matches that. JS has different behaviour for Number.toFixed and Math.round. In case you want to have rounding based on Math.round, that can be done on consumer land.

<NumberFormat
  value ={Math.round(0.175 * 100) / 100}
  decimalScale={2}
/>