Reference · Math

math.trunc

signature
math.trunc(num) -> int

What it does

The number with its fraction dropped, as an int: rounding toward zero, as Python's math.trunc. math.trunc(3.7) is 3 and math.trunc(-3.7) is -3. An int is itself. It is what int does to a float, but a value that is not a number is a runtime error here rather than an error value.

Examples

print(math.trunc(3.7), math.trunc(-3.7), math.trunc(7))print(math.trunc(2.9) == int(2.9), type_of(math.trunc(2.9)))
output
3 -3 7true int

Edge cases

print(math.trunc(-0.5), math.trunc(-7 / 2))print(math.trunc(0.999), math.trunc(-0.999))print(math.trunc(9.2e18), math.trunc(9223372036854775807 / 10))
output
0 -30 09200000000000000000 922337203685477580
this one fails
print(math.trunc(-1e19))
output
line 1: integer overflow

Errors

Infinity and NaN cannot be an int and are a runtime error, not an error value; so is null or any other value that is not a number. A decimal is refused with a hint to convert it with float.

this one fails
print(math.trunc(null))
output
line 1: `math.trunc` expects a number as argument 1, got null

Chaining

In a pipeline the number comes first:

print(7.9 | math.trunc)print([1.5, -1.5, 2.5] | map(math.trunc))
output
7[1, -1, 2]

See also

math.floor, math.ceil, math.modf, int, round

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On math.trunc(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo