Skip to main content

Integer.round

rounddecimal_placesuse_bankers

Group: Rounding

Documentation

Round to a specified number of decimal places. For integers, rounding to 0 or more decimal places simply returns the argument. For negative decimal places, see below. 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".

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 an integer (returns the value unchanged).

     3 . round == 3

Round to the nearest thousand.

     2511 . round -3 == 3000

Round to the nearest hundred, using Banker's Rounding.

     12250 . round -2 use_bankers=True == 12200

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.