Math.round
func Math.round(x: float64): float64Math.round returns x rounded to the nearest whole number, with halves rounded away from zero.
Details
- The result is a
float64, not anint. Convert it with int, which panics when the value is out of range, or with Math.toInt, which returnsnullinstead. - A half goes away from zero:
2.5gives3.0and-2.5gives-3.0. It is not rounded to even.
Basic examples
Round to the nearest whole number:
Math.round(2.4) // 2.0
Math.round(2.6) // 3.0A half goes away from zero, on both sides:
Math.round(2.5) // 3.0
Math.round(-2.5) // -3.0Convert the result to get an int:
int(Math.round(7.5)) // 8Scope
Halves go away from zero at every size, even when the even neighbor is closer to zero:
[0.5, 1.5, 2.5, -0.5].map(Math.round) // [1.0, 2.0, 3.0, -1.0]A negative number that rounds to zero keeps its sign, and -0.0 equals 0.0:
Math.round(-0.4) // -0.0
Math.round(-0.4) == 0.0 // trueThe largest float64 below 0.5 rounds down, as it is not a half:
Math.round(0.49999999999999994) // 0.0NaN and the infinities come back unchanged:
Math.round(Math.NaN) // NaN
Math.round(-Math.Infinity) // -InfinityEvery float64 from 2^52 up is already whole, so a large number comes back as it is:
Math.round(4503599627370497.0) // 4503599627370497.0
Math.round(1e300) // 1e+300The parameter is named x, and the function works in a pipe:
Math.round(x: 1.5) // 2.0
3.7 | Math.round() // 4.0Applications
Round ratings to whole stars:
let ratings = [4.2, 3.5, 4.8, 2.49]
ratings.map({ int(Math.round(x)) }) // [4, 4, 5, 2]Round to a number of decimals by scaling first:
func roundTo(x: float64, decimals: int): float64 {
let scale = Math.pow(10.0, float64(decimals))
Math.round(x * scale) / scale
}
roundTo(3.14159, 2) // 3.14
roundTo(2.675, 2) // 2.68
roundTo(1234.5, -2) // 1200.0Report a percentage as a whole number:
type Poll = { yes: int, total: int }
func percent(p: Poll): int {
int(Math.round(float64(p.yes) * 100.0 / float64(p.total)))
}
let polls = [Poll{ yes: 1, total: 3 }, Poll{ yes: 2, total: 3 }, Poll{ yes: 1, total: 8 }]
for p in polls {
println("${percent(p)}%")
}33%
67%
13%Properties and relations
The result is never more than one half away from x:
for x in [2.4, 2.5, -2.5, -0.4, 7.0] {
assert(Math.abs(Math.round(x) - x) <= 0.5)
}Rounding is symmetric around zero:
for x in [0.5, 1.4, 2.5, 3.6] {
assert(Math.round(-x) == -Math.round(x))
}The result is always between Math.floor and Math.ceil:
let x = -3.7
assert(Math.floor(x) <= Math.round(x) && Math.round(x) <= Math.ceil(x))Possible issues
The argument must be a float64. An int is a compile error, since numeric types never mix:
let n = 7
Math.round(n) // error: cannot use int where float64 is expectedInteger division has already dropped the fraction, so convert before dividing:
7 / 2 // 3
Math.round(float64(7) / 2.0) // 4.0A decimal that looks like a half may sit just below it in binary, and rounds down:
1.005 * 100.0 // 100.49999999999999
Math.round(1.005 * 100.0) // 100.0Adding 0.5 and taking Math.floor is not the same: the sum can round up before Math.floor sees it.
Math.floor(0.49999999999999994 + 0.5) // 1.0
Math.round(0.49999999999999994) // 0.0Math.round and String.fromFloat round halves differently: fromFloat rounds half to even.
Math.round(2.5) // 3.0
String.fromFloat(2.5, 0) // "2"A whole float64 may still be too large for an int, so int(...) panics on it. Use Math.toInt to get null instead:
int(Math.round(1e20)) // panic: 100000000000000000000.0 out of range for int
Math.toInt(Math.round(1e20)) // nullSee also
Math.floor · Math.ceil · Math.trunc · Math.toInt · String.fromFloat · int
