Built-in routines
Rounding — floor / ceiling / round / truncate
FullRound to integers or a precision, with the half-way rule Rakudo uses.
Four routines turn a fractional number into a whole one: floor (down), ceiling (up), truncate (toward zero), and round (to nearest).
floor, ceiling, truncate #
say 3.7.floor; say 3.2.ceiling; say 3.7.truncate; say (-3.7).floor;
Output
3
4
3
-4floor goes toward −∞, ceiling toward +∞, truncate toward zero (so (-3.7).truncate is -3, while (-3.7).floor is -4).
round to a precision #
round takes an optional scale — round to the nearest multiple of it.
say 3.5.round; say 3.14159.round(0.01);
Output
4
3.14Halves round toward +∞ #
For a value exactly halfway, round rounds toward +∞ — so a positive .5 goes up and a negative .5 goes toward zero.
say 3.5.round; say (-3.5).round; say (-2.5).round;
Output
4
-3
-23.5 rounds up to 4; -3.5 rounds up to -3 (not -4).
Notes #
- The half-way rule only matters for values ending exactly in
.5; every other value rounds to the genuinely nearest integer. floor,ceiling, andtruncatehave no half-way ambiguity..Inton a fractional number truncates toward zero — the same as.truncate.