Float.round
Group: Rounding
Documentation
Round to a specified number of decimal places. By default, rounding uses "symmetric round-half-up", also known as "half-up." If use_bankers=True, then it uses "round-half-even", also known as "banker's rounding". If decimal_places
> 0, round
returns a Float
; otherwise, it returns an Integer
. If the argument is NaN
or +/-Inf
, an Arithmetic_Error
error is thrown.
Arguments
decimal_places
: The number of decimal places to round to. Can be negative, which results in rounding to positive integer powers of 10. Must be between -15 and 15 (inclusive).use_bankers
: Rounds mid-point to nearest even number.
Examples
Round to the nearest integer.
3.3 . round == 3
Round to two decimal places.
3.1415 . round 2 == 3.14
Round to the nearest hundred.
1234.0 . round -2 == 1200
Use Banker's Rounding.
2.5 . round use_bankers=True == 2
Errors
If self
is outside the range -99999999999999..99999999999999
(inclusive), an Illegal_Argument
error is thrown.
If decimal_places
is outside the range -15..15 (inclusive), an
Illegal_Argument
error is thrown.
Remarks
Negative decimal place counts
Rounding to n
digits can be thought of as "rounding to the nearest
multiple of 10^(-n)". For negative decimal counts, this results in
rounding to the nearest positive integer power of 10.