Rounding, Ceiling, and Floor in Python with Decimal: A Complete Guide to quantize and Rounding Modes

Python’s standard round() function adopts “Round half to even” (Banker’s rounding), so it may produce results different from the standard “rounding half up” method you learn in school.

When you need fractional processing based on strict rules, such as in financial calculations, you should use the standard library decimal module. By using the quantize() method of this module, you can accurately control not only standard rounding but also rounding up (ceiling) and rounding down (floor).

This article explains the list of rounding modes available in the decimal module and how to implement rounding for both decimal places and integer parts.

目次

List of Decimal Rounding Modes

The decimal module defines the following constants. You can specify how to round by passing them as arguments to the quantize() method.

Constant NameMeaningBehavior Example (Positive Numbers)
ROUND_HALF_UPStandard RoundingRounds to the nearest number. Ties (0.5) round away from zero (round up).
ROUND_HALF_DOWNRound Half DownRounds to the nearest number. Ties (0.5) round towards zero (round down).
ROUND_HALF_EVENRound Half to EvenRounds to the nearest number. Ties (0.5) round to the nearest even number. Same as Python’s standard round().
ROUND_UPRound UpRounds away from zero (Ceiling for absolute values).
ROUND_DOWNRound DownRounds towards zero (Floor for absolute values).
ROUND_CEILINGCeilingRounds towards positive infinity (always rounds to the larger number).
ROUND_FLOORFloorRounds towards negative infinity (always rounds to the smaller number).
ROUND_05UPSpecialRounds so the last digit is 0 or 5.

Generally, ROUND_HALF_UP is the most commonly used method for standard rounding.

1. Rounding Decimal Places

Pass a Decimal object representing the number of digits (precision) you want to round to as the first argument of the quantize() method.

  • Round to integer: Decimal("0")
  • Round to 1 decimal place: Decimal("0.1")
  • Round to 2 decimal places: Decimal("0.01")
from decimal import Decimal, ROUND_HALF_UP

# Original value (specified as a string to prevent errors)
target_value = Decimal("123.4567")

print(f"Original: {target_value}")

# 1. Round to integer (ones place)
# Specifying "0" results in an integer
val_int = target_value.quantize(Decimal("0"), rounding=ROUND_HALF_UP)
print(f"Integer: {val_int}")

# 2. Round to 1 decimal place
# Specify "0.1"
val_1 = target_value.quantize(Decimal("0.1"), rounding=ROUND_HALF_UP)
print(f"1 decimal place: {val_1}")

# 3. Round to 2 decimal places
# Specify "0.01"
val_2 = target_value.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
print(f"2 decimal places: {val_2}")

# 4. Round to 3 decimal places
val_3 = target_value.quantize(Decimal("0.001"), rounding=ROUND_HALF_UP)
print(f"3 decimal places: {val_3}")

Execution Result:

Original: 123.4567
Integer: 123
1 decimal place: 123.5
2 decimal places: 123.46
3 decimal places: 123.457

2. Rounding Integer Parts (Tens, Hundreds)

If you want to round at an integer place (like the tens or hundreds place), use Scientific Notation (E-notation) for the argument of quantize().

  • To the tens place (rounding the ones digit): Decimal("1E1")
  • To the hundreds place (rounding the tens digit): Decimal("1E2")
from decimal import Decimal, ROUND_HALF_UP

# Integer to be rounded
large_value = Decimal("56789")

print(f"Original: {large_value}")

# 1. Round to the tens place (1E1 = 10)
# 56789 -> 56790
round_10 = large_value.quantize(Decimal("1E1"), rounding=ROUND_HALF_UP)
print(f"Tens place: {round_10}")

# 2. Round to the hundreds place (1E2 = 100)
# 56789 -> 56800
round_100 = large_value.quantize(Decimal("1E2"), rounding=ROUND_HALF_UP)
print(f"Hundreds place: {round_100}")

# 3. Round to the thousands place (1E3 = 1000)
# 56789 -> 57000
round_1000 = large_value.quantize(Decimal("1E3"), rounding=ROUND_HALF_UP)
print(f"Thousands place: {round_1000}")

Execution Result:

Original: 56789
Tens place: 5.679E+4
Hundreds place: 5.68E+4
Thousands place: 5.7E+4

Returning Results to Standard Number Notation

As shown above, when rounding large digits, the result may become scientific notation (like 5.68E+4). To convert this back to a standard numeric string, use int() or a formatted string.

# Display in standard notation
print(f"Hundreds place (int conversion): {int(round_100)}")
print(f"Thousands place (f-string): {round_1000:.0f}")

Execution Result:

Hundreds place (int conversion): 56800
Thousands place (f-string): 57000

Summary

  • Use the decimal module for strict rounding.
  • Pass a Decimal object representing the precision and a rounding mode constant (such as ROUND_HALF_UP) to the quantize() method.
  • Decimal rounding: Write like Decimal("0.01").
  • Integer rounding: Write using scientific notation like Decimal("1E2").
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次