Multiplying Decimals
To multiply decimals, multiply as if there were no decimal points, then put the point back: the answer has as many decimal places as the two factors together. This lesson explains that rule, multiplying by powers of ten, estimating, and how Decimal handles prices times quantities and tax rates.
The rule
- Ignore the decimal points and multiply the whole numbers.
- Count the decimal places in both factors together.
- Give the answer that many decimal places.
Why it works: and , so the product is . The denominators multiply, so the decimal places add up.
| Problem | Whole-number product | Places | Answer |
|---|---|---|---|
| 6 | 2 | 0.06 | |
| 60 | 1 | 6.0 | |
| 63 | 3 | 0.063 | |
| 1000 | 3 |
Note: , not 0.6. Multiplying two numbers smaller than 1 gives something smaller than both, just like with fractions.
Multiplying by 10, 100, 1000
Each factor of 10 moves the decimal point one place to the right:
Multiplying by 0.1, 0.01, 0.001 moves it to the left: . That is the same as dividing by 100. Moving the point is how scientific notation works.
Estimate to place the point
Round each factor to one significant digit: . The exact product 103.731 is close; 10.3731 or 1037.31 would mean a misplaced point.
Decimal places in Python's Decimal
Decimal follows the paper rule exactly: the result's exponent is the sum of the factors' exponents.
from decimal import Decimal as D
print(D("2.4") * D("0.35"))
print(D("0.3") * D("0.2"))
print(D("12.5") * D("0.08"))
print(D("3.142") * 100)
print(0.3 * 0.2) # float: close, not exact
print(1.1 * 1.1)0.840
0.06
1.000
314.200
0.06
1.2100000000000002
Notice D("2.4") * D("0.35") printed 0.840 with three places, exactly what the rule predicts. The float 1.1 * 1.1 shows the usual binary error.
, and tax
A typical invoice line is , then a tax rate on top. Multiplication produces more decimal places than money has, so you round to cents at a defined point (rounding itself is covered in Rounding and Repeating Decimals):
from decimal import Decimal, ROUND_HALF_UP
CENT = Decimal("0.01")
lines = [("notebook", "3.49", 4), ("pen", "1.25", 3), ("bag", "18.90", 1)]
rate = Decimal("0.0825") # 8.25 % sales tax
subtotal = Decimal("0")
for name, price, qty in lines:
amount = Decimal(price) * qty
subtotal += amount
print(f"{name:9} {qty} x {price:>6} = {amount:>7}")
tax = (subtotal * rate).quantize(CENT, rounding=ROUND_HALF_UP)
print("subtotal:", subtotal)
print("tax raw: ", subtotal * rate, " rounded:", tax)
print("total: ", subtotal + tax)notebook 4 x 3.49 = 13.96
pen 3 x 1.25 = 3.75
bag 1 x 18.90 = 18.90
subtotal: 36.61
tax raw: 3.020325 rounded: 3.02
total: 39.63
The raw tax has decimal places, again the counting rule. quantize(CENT) rounds it to whole cents.
In software
- Invoices and carts: . Where exactly you round (per line or on the total) is a business rule, and tests must check it with exact
Decimalvalues. - Unit scaling: converting , or , is decimal multiplication.
- Fixed-point arithmetic: some games and embedded systems store numbers as integers scaled by 1000 (or ). Multiplying two such numbers gives a result scaled by , so the code divides by 1000 once, the integer version of "add the decimal places".
Exercises
Exercise 1: Count the places
Compute and by hand.
Answer
0.024 and 7.7
Solution
Exercise 2: Powers of ten
Compute and .
Answer
7.2 and 0.456
Solution
moves the point 3 places right: . moves it 2 places left: .
Exercise 3: Fuel cost
A car uses 6.4 litres of fuel per 100 km. Fuel costs 1.85 per litre. What does fuel for 250 km cost?
Answer
29.60
Solution
from decimal import Decimal as D
print(D("6.4") * D("2.5") * D("1.85"))29.6000
Decimal kept every place the rule produces: ( places), then gives places, 29.6000. In money terms that is 29.60.
Exercise 4: Fixed-point multiply
Store prices as integers in thousandths (). Write fx_mul(a, b) that multiplies two such numbers and returns a result in thousandths (truncating). Test .
Answer
3900 (meaning 3.900)
Solution
, which is scaled by . Divide by 1000 once to get back to thousandths: 3900.
SCALE = 1000
def fx_mul(a, b):
return a * b // SCALE
r = fx_mul(3250, 1200)
print(r, "->", r / SCALE)3900 -> 3.9
Test yourself
What is ?
- 2.5
- 0.25
- 0.025
- 1.0
How many decimal places does have before dropping trailing zeros?
- 2
- 3
- 6
- 5
What is ?
- 0.0725
- 72.5
- 725
- 7250