ParseFloat function in JavaScript

前端 未结 5 740
暖寄归人
暖寄归人 2021-02-14 04:09

When I am adding two textbox values that are 1.001 and 0.001 and then I do a parseFloat I get 1.0019999999. I wan

5条回答
  •  悲&欢浪女
    2021-02-14 04:47

    0.002 cannot be accurately represented as a base 2 number. Similar to the way that 1/3 can't be represented in base 10.

    1/3 = 0.33333... recuring. To represent the number accurately in base 10, you would need an infinite number of decimal digits.

    0.002 is a number that can be accurately represented in base 10 (as we see here), but not in base 2, as used by computers. To represent this number accurately, would take an infinite number of binary digits.

提交回复
热议问题