Dividing Decimals
Dividing by a decimal becomes ordinary long division once you move the decimal points so the divisor is a whole number. This lesson shows that trick, dividing by powers of ten, what happens when a division never ends, and how to split money fairly in code without losing a cent.
Make the divisor a whole number
Multiplying both numbers in a division by the same amount does not change the answer (it is the same idea as equivalent fractions):
So: move the point in the divisor right until it is a whole number, and move the point in the dividend right by the same number of places (adding zeros if needed). Then divide.
| Problem | Shift both by | Becomes | Answer |
|---|---|---|---|
| 2 places | 30 | ||
| 1 place | 9 | ||
| 3 places | 90 | ||
| none | 4.2 |
When the divisor is already whole (), divide normally and put the point in the answer straight above the point in the dividend:
4.2
┌────
3 │12.6
12
─
0 6
6
─
0Tip: Dividing by a number less than 1 makes the result bigger: . You are asking how many 0.15s fit into 4.5, and many small pieces fit.
Dividing by 10, 100, 1000
Each factor of 10 moves the point one place left: , . This is the reverse of multiplying.
Divisions that never end
Some divisions stop (), others repeat forever (). A decimal stops exactly when the fraction in lowest terms has a denominator made only of 2s and 5s, the prime factors of 10. More on this in Rounding and Repeating Decimals.
Decimal stops after a fixed number of significant digits (28 by default), and you can change the precision locally:
from decimal import Decimal as D, localcontext
print(D("4.5") / D("0.15"))
print(D("12.6") / 3)
print(D(1) / D(8))
print(D(1) / D(3)) # 28 significant digits by default
with localcontext() as ctx:
ctx.prec = 6
print(D(1) / D(3), D(200) / D(7))3E+1
4.2
0.125
0.3333333333333333333333333333
0.333333 28.5714
Surprise: D("4.5") / D("0.15") printed 3E+1. That is scientific notation for . Decimal keeps track of how many digits are significant and chose that form; .normalize() or formatting with f"{x:f}" gives the plain 30.
Note:
localcontext()changes precision only inside thewithblock. Changinggetcontext().precglobally affects every later calculation in the program, which is easy to forget.
Splitting a bill fairly
Divide 100.00 between 3 people: A computer cannot pay 33.333… each, and rounding everyone to 33.33 loses a cent (). The standard fix is to work in whole cents, divide with remainder, and give the leftover cents to the first people:
from decimal import Decimal
def split(total, people):
cents = int(total * 100)
share, extra = divmod(cents, people)
parts = [share + 1 if i < extra else share for i in range(people)]
return [Decimal(p) / 100 for p in parts]
for total, n in [(Decimal("100.00"), 3), (Decimal("59.99"), 4), (Decimal("10.00"), 6)]:
parts = split(total, n)
print(total, "/", n, "->", [str(p) for p in parts], " sum:", sum(parts))100.00 / 3 -> ['33.34', '33.33', '33.33'] sum: 100.00
59.99 / 4 -> ['15', '15', '15', '14.99'] sum: 59.99
10.00 / 6 -> ['1.67', '1.67', '1.67', '1.67', '1.66', '1.66'] sum: 10.00
The sums always match the total. (Dividing an integer Decimal by 100 gives 15 rather than 15.00; for display you would format with two places.)
In software
- Bill splitting, instalments and pro-rata refunds all need the "divide, then distribute the remainder" pattern. Libraries call it allocation (for example
Money.allocatein many money libraries). - Averages: is a division that usually does not end; decide the precision you report.
- Rates per unit: price per 100 g, cost per click, bytes per second. These are divisions where the divisor is often a decimal.
Exercises
Exercise 1: Shift the points
Compute .
Answer
24
Solution
Check: .
Exercise 2: Long division with a decimal answer
Compute .
Answer
1.35
Solution
The divisor 7 is already whole. ; bring down 4: ; bring down 5: . With the point above the point: 1.35. Check: .
Exercise 3: Unit price
A 0.75 kg bag of coffee costs 13.50. What is the price per kilogram?
Answer
18.00 per kg
Solution
Exercise 4: Split a subscription
A team of 7 shares a 250.00 yearly subscription. Use the split idea from the lesson to print each share with two decimals, and check the sum.
Answer
Three people pay 35.72 and four pay 35.71; the sum is 250.00.
Solution
, so 3 people get one extra cent (3572) and the other 4 get 3571:
cents, n = 25000, 7
share, extra = divmod(cents, n)
parts = [share + 1 if i < extra else share for i in range(n)]
print([f"{p / 100:.2f}" for p in parts], f"sum: {sum(parts) / 100:.2f}")['35.72', '35.72', '35.72', '35.71', '35.71', '35.71', '35.71'] sum: 250.00
Check: .
Test yourself
What is ?
- 0.7
- 70
- 7
- 5.67
Which fraction gives a decimal that ends (terminates)?
If each of 3 people pays the rounded share 3.33, the total collected is…
- 10.00
- 9.99
- 10.01
- 9.00