1

What is the reason behind this type of conversion and how should I convert string lua floating point string to number using tonumber?

tonumber('22.31')
--22.310000000000002
tonumber('22.32')
--22.32
tonumber('22.33');
--22.330000000000002
tonumber('22.34');
--22.34
tonumber(22.31)
--same result for number as well
--22.310000000000002
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
anandaravindan
  • 2,401
  • 6
  • 25
  • 35
  • 2
    Nothing wrong with using just tonumber. Why is tonumber not good enough for you? Do you have some requirements you need to meet with the string to number conversion? Also, not all values between 0 and 1 can be represented by a float, there are infinite amount of numbers in that range. – Rochet2 Mar 04 '16 at 22:45
  • I do not have any specific use case other than visually its bit odd to see a long number for 22.31. But I just want to understand why 22.31 is converted to bit long number and why not 22.32 or 22.34. – anandaravindan Mar 04 '16 at 22:53
  • Possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Colonel Thirty Two Mar 06 '16 at 19:17

2 Answers2

4

Most of the floating point numbers can NOT be represented precisely in binary.

Some floating point numbers are precise in binary, like 22.5, 22.25, 22.125, etc. However, the numbers in your examples are not one of these.

So why did some of these like 22.32 or 22.24 are shown without the trailing part while others do? It's only because of the display precision.

string.format('%.20g', tonumber('22.32'))
--> 22.320000000000000284
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
0

I just want to understand why 22.31 is converted to bit long number and why not 22.32 or 22.34

There are infinitely many numbers between 0 and 1. If you have a number 0.1 you could always add 1 to the end of it infinite amount of times thus generating infinite amount of numbers.

Not all values between 0 and 1 can be represented by a float since float can not hold infinite amount of data. Float is more of an approximation than an exact value.

You may find this SO answer helpful: https://stackoverflow.com/a/1089026/3586583

Community
  • 1
  • 1
Rochet2
  • 1,146
  • 1
  • 8
  • 13