Skip to main content

Decimal.round

rounddecimal_placesuse_bankers

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".

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 Java Integer.MIN_VALUE and Integer.MAX_VALUE (-2147483648 and 2147483647) (inclusive).
  • use_bankers: Rounds mid-point to nearest even number.

Examples

Round to the nearest integer.

      Decimal.new "3.3" . round
# => Decimal.new "3"

Round to two decimal places.

      Decimal.new "3.1415" . round 2
# => Decimal.new "3.14"

Round a very large number.

      Decimal.new "1234.5678E-50" . round 53
# => Decimal.new "1234.568E-50"

Use Banker's Rounding.

      Decimal.new "2.5" . round use_bankers=True
# => 2

Errors

If decimal_places is outside the range Integer.MIN_VALUE and Integer.MAX_VALUE (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.