floating point issue

前端 未结 4 1848
醉梦人生
醉梦人生 2020-12-12 07:19

I have a floating value as 0.1 entering from UI.

But, while converting that string to float i am getting as 0.10...01. The problem is the appending of non zero dig

4条回答
  •  执笔经年
    2020-12-12 07:46

    Since floats get stored in binary, the fractional portion is effectively in base-two... and one-tenth is a repeating decimal in base two, same as one-ninth is in base ten.

    The most common ways to deal with this are to store your values as appropriately-scaled integers, as in the C# or SQL currency types, or to round off floating-point numbers when you display them.

提交回复
热议问题