1.String To Number
- All arithmetic operations on a string attempt to convert the string to a number.
- Lua5.3 didn’t realize the integration of the casts and integer, it adopted a more simple and rapid way of implementation: the rules of arithmetic operation is only when the two operands are integer result is integer, therefore, because the string is not integer value, so any string in arithmetic will be regarded as floating point arithmetic processing.
- You can use the function tonumber to explicitly convert a string to a number. By default, the tonumber function uses decimal notation, but can also use any base for the first argument:
For example, tonumber(” 100101 “,2) –> 37
2.Number To Number
- The toString function is called to convert a numeric value to a string
print(tostring(10) == "10") --> true
- This conversion is always valid, but it does not control the format of the output string (for example, the number of decimal digits in the result)
3.int To float
- Integer values can be converted to floating point by incrementing 0.0
Print (-3 + 0.0) --> -3.0
-
Disadvantages: Possible loss of precision for integer values with absolute values greater than 2^53(9007199254740992)
Print (9007199254740991 + 0.0 == 9007199254740991) -> true print(900719925740993 + 0.0 == 900719925470993) –> false
4.float To int
-
You can cast a floating point value to an integer value by performing a bitwise or operation with zero:
2 ^ 53 – > 9.007199254741 e+15 (float type) 2 ^ 53 | 0 – – > 9007199254740992 (integer value)
-
Disadvantages: When you force a floating-point value to an integer value, the Lua language checks that the value is exactly the same as the integer value representation. Conditions: that is, there is no decimal part and the value is in the range of integer values. An exception is thrown if the condition is not met
| 0-3.2 the decimal part stdin: 1: the number has no integer reoresentation 2 ^ 64 | 0 – beyond the range
-
You can cast a numeric value to an integer by using the function math.toINTEGER.