Quirrel4.41.0

types.Integer.tofloat

Method on every integer
pure (bool|number).tofloat(): any

Converts 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

examples/types/integer/tofloat.nut
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 = false

See also

tointegerReturns the integer itself.
tocharConverts the integer to a one-character string, treating it as a character code.
tofloatReturns the float itself.
tofloatConverts the bool to a float.
integerclass index