types.Integer.tofloat
Method on every
integerConverts the integer to a float.
Return value
The nearest float to the integer's value.
Errors
Nothing throws here, but the conversion is lossy: SQFloat is a 32-bit float in this build, while SQInteger is 64-bit, so an integer needs no more than about 7 significant decimal digits before tofloat() cannot represent it exactly any more. 9007199254740993 (2^53 + 1) round-trips through a double in most languages but not through this 32-bit float: tofloat() rounds it to the nearest representable value, and converting that back with tointeger does not reproduce the original integer.
Notes
Takes no arguments. See types.Float.tofloat, which is the identity conversion and does not round.
Example
println("(42).tofloat() =", (42).tofloat())
println("type((42).tofloat()) =", type((42).tofloat()))
// SQFloat is a 32-bit float in this build, so an integer with more than
// about 7 significant decimal digits cannot round-trip through tofloat().
let big = 9007199254740993 // 2^53 + 1
println("big.tofloat() =", big.tofloat())
println("big.tofloat().tointeger() == big =", big.tofloat().tointeger() == big)Output:
(42).tofloat() = 42
type((42).tofloat()) = float
big.tofloat() = 9.0072e+15
big.tofloat().tointeger() == big = falseSee also
tointeger | Returns the integer itself. |
tochar | Converts the integer to a one-character string, treating it as a character code. |
tofloat | Returns the float itself. |
tofloat | Converts the bool to a float. |
| integer | class index |