Math.trunc
func Math.trunc(x: float64): float64Math.trunc returns x with its fraction dropped, rounding toward zero. int(Math.trunc(x)) is the explicit truncating conversion to int.
Details
- The result is a
float64, not anint.int(Math.trunc(x))turns it into anint; a plainint(x)panics whenxhas a fraction. - It never panics.
NaNand the infinities come back unchanged, and a negative number above-1.0gives-0.0, which equals0.0.
Basic examples
Drop the fraction, on either side of zero:
Math.trunc(2.7) // 2.0
Math.trunc(-2.7) // -2.0
Math.trunc(5.0) // 5.0Convert the result to get an int:
int(Math.trunc(9.99)) // 9Scope
A negative number between -1.0 and 0.0 gives -0.0, which equals 0.0:
Math.trunc(-0.5) // -0.0
Math.trunc(-0.5) == 0.0 // trueNaN and the infinities come back as they are:
Math.trunc(Math.NaN) // NaN
Math.trunc(-Math.Infinity) // -InfinityEvery float64 from 2^52 up is already whole, so a large number comes back unchanged:
Math.trunc(1e300) // 1e+300The parameter is named x, and the function works in a pipe:
Math.trunc(x: -1.5) // -1.0
3.99 | Math.trunc() // 3.0Math.trunc is a function value, so it can be passed to Array.map:
[1.9, -1.9, 0.5].map(Math.trunc) // [1.0, -1.0, 0.0]Applications
Convert a measured float to an int, dropping the fraction:
let readings = [12.8, 7.2, -3.6]
readings.map({ int(Math.trunc(x)) }) // [12, 7, -3]Split a number into its whole part and its fraction, which keeps the sign of x:
type Parts = { whole: float64, fraction: float64 }
func split(x: float64): Parts {
let whole = Math.trunc(x)
Parts{ whole, fraction: x - whole }
}
split(3.25) // Parts{ whole: 3.0, fraction: 0.25 }
split(-3.25) // Parts{ whole: -3.0, fraction: -0.25 }Show a duration in seconds as whole minutes and seconds:
func clock(seconds: float64): string {
let total = int(Math.trunc(seconds))
"${total / 60}:${String.from(total % 60).padStart(2, "0")}"
}
clock(125.9) // "2:05"
clock(59.99) // "0:59"Cut a value to a number of decimals, toward zero on both sides:
func cut(x: float64, decimals: int): float64 {
let scale = Math.pow(10.0, float64(decimals))
Math.trunc(x * scale) / scale
}
cut(3.14159, 2) // 3.14
cut(-3.14159, 2) // -3.14Properties and relations
The result is whole, has the sign of x, and is less than one away from it:
for x in [2.7, -2.7, 0.5, 5.0, -9.99] {
let t = Math.trunc(x)
assert(Math.trunc(t) == t)
assert((t >= 0.0) == (x >= 0.0) || t == 0.0)
assert(Math.abs(x - t) < 1.0 && Math.abs(t) <= Math.abs(x))
}It is Math.floor for zero and above and Math.ceil below zero:
for x in [2.5, -2.5, 0.0, 7.0] {
let expected = if x >= 0.0 { Math.floor(x) } else { Math.ceil(x) }
assert(Math.trunc(x) == expected)
}It is symmetric around zero:
for x in [0.5, 1.4, 2.5, 3.6] {
assert(Math.trunc(-x) == -Math.trunc(x))
}For whole operands, it agrees with integer /, which also truncates toward zero:
for a in [7, -7, 9, -9] {
assert(int(Math.trunc(float64(a) / 2.0)) == a / 2)
}Possible issues
int(x) does not truncate. It panics when x has a fraction:
int(2.7) // panic: 2.7 has a fraction, so it does not convert to int
int(Math.trunc(2.7)) // 2The argument must be a float64. An int is a compile error, since numeric types never mix:
let n = 7
Math.trunc(n) // error: cannot use int where float64 is expectedA product that looks whole can sit just below the whole number in binary, so Math.trunc gives one less than you expect:
4.35 * 100.0 // 434.99999999999994
Math.trunc(4.35 * 100.0) // 434.0To round to the nearest whole number instead, use Math.round:
Math.trunc(2.9) // 2.0
Math.round(2.9) // 3.0A whole float64 may still be too large for an int, so int(...) panics on it. Use Math.toInt to get null instead:
int(Math.trunc(1e20)) // panic: 100000000000000000000.0 out of range for int
Math.toInt(Math.trunc(1e20)) // nullSee also
Math.floor · Math.ceil · Math.round · Math.toInt · int · Arithmetic operators
