Skip to content

Rounding

The rite.numeric.rounding submodule includes helpers for rounding values, computing floors and ceilings, and truncating.

Rounding Module

Numeric rounding utilities.

This submodule provides utilities for rounding operations like round, floor, ceil, and truncate.

Examples

from rite.numeric.rounding import ( ... rounding_round, ... rounding_floor ... ) rounding_round(3.14159, 2) 3.14 rounding_floor(3.9) 3

Modules

rounding_ceil

Ceil Function

Round up to nearest integer.

Examples

from rite.numeric.rounding import rounding_ceil rounding_ceil(3.1) 4

Functions

rounding_ceil(value: float) -> int

Round up to nearest integer.

Parameters:

Name Type Description Default
value float

Number to round up.

required

Returns:

Type Description
int

Ceiling integer value.

Examples:

>>> rounding_ceil(3.1)
4
>>> rounding_ceil(3.9)
4
>>> rounding_ceil(-3.1)
-3
Notes

Rounds toward positive infinity.

rounding_floor

Floor Function

Round down to nearest integer.

Examples

from rite.numeric.rounding import rounding_floor rounding_floor(3.9) 3

Functions

rounding_floor(value: float) -> int

Round down to nearest integer.

Parameters:

Name Type Description Default
value float

Number to round down.

required

Returns:

Type Description
int

Floored integer value.

Examples:

>>> rounding_floor(3.9)
3
>>> rounding_floor(3.1)
3
>>> rounding_floor(-3.1)
-4
Notes

Rounds toward negative infinity.

rounding_round

Round Function

Round number to specified decimal places.

Examples

from rite.numeric.rounding import rounding_round rounding_round(3.14159, 2) 3.14

Functions

rounding_round(value: float, decimals: int = 0) -> float

Round number to specified decimal places.

Parameters:

Name Type Description Default
value float

Number to round.

required
decimals int

Number of decimal places.

0

Returns:

Type Description
float

Rounded value.

Examples:

>>> rounding_round(3.14159, 2)
3.14
>>> rounding_round(3.5)
4.0
>>> rounding_round(123.456, 1)
123.5
Notes

Uses banker's rounding (round half to even).

rounding_trunc

Truncate Function

Truncate decimal portion of number.

Examples

from rite.numeric.rounding import rounding_trunc rounding_trunc(3.9) 3

Functions

rounding_trunc(value: float) -> int

Truncate decimal portion of number.

Parameters:

Name Type Description Default
value float

Number to truncate.

required

Returns:

Type Description
int

Truncated integer value.

Examples:

>>> rounding_trunc(3.9)
3
>>> rounding_trunc(3.1)
3
>>> rounding_trunc(-3.9)
-3
Notes

Rounds toward zero.

options: show_root_heading: true show_source: false heading_level: 2