Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rounding (Rational)

Examples on this page assume from decimo.prelude import * and run inside def main() raises:.

The four roundings all return a Rational with a denominator of one, and the standard library’s functions take this type:

from std.math import ceil, floor, trunc

var half_down = Rational(-7, 2)
print(floor(half_down))                    # -4
print(ceil(half_down))                     # -3
print(trunc(half_down))                    # -3, toward zero
print(round(half_down))                    # -4
print(round(Rational(5, 2)))               # 2, halves go to the even side
print(round(Rational(1, 8), 2))            # 3/25, which is 0.12

round(x, n) takes a negative n to round above the decimal point, as Python’s does: round(Rational(1350), -2) is 1400.

Built from decimo main. This page in the manual.