Money
Why 0.1 + 0.2 is not 0.3, why money is kept as whole cents, how rounding rules differ, and how to split 100.00 into three shares without losing a cent.
What you will learn#
- why fractions are inexact in every computer language,
- the cents rule: whole numbers inside, decimals only for display,
- rounding rules: banker's rounding and school rounding,
- splitting an amount fairly,
- taxes on the total, not on each line.
The problem#
Computers store numbers in binary. Just as 1/3 cannot be written exactly in decimal (0.3333…), 0.1 cannot be written exactly in binary, so 0.1 + 0.2 is 0.30000000000000004 and is not equal to 0.3. Ten additions of 0.1 do not give exactly 1. For physics this is fine; for money it is not: a shop must never be one cent off.
The program#
# money - why 0.1 + 0.2 is not 0.3, cents as whole numbers, rounding rules and fair splitting
load "dotnet.ring"
using {
"System"
}
func main
# 1. Computers store fractions in binary: 0.1 and 0.2 cannot be written exactly, so tiny errors appear
? "1. 0.1 + 0.2 = 0.3 ? " + yesNo((0.1 + 0.2) = 0.3)
nTotal = 0
for nI = 1 to 10
nTotal += 0.1
next
? " ten times 0.1 equals 1 ? " + yesNo(nTotal = 1)
# 2. The rule for money: keep whole CENTS (integers) and only turn them into "12.50" when you show them
nPriceCents = toCents("19.99")
nTotalCents = nPriceCents * 3
? nl + "2. three items of 19.99 = " + showMoney(nTotalCents)
# 3. Percentages need a rounding rule. The tax of 7.5 % on 19.99 is 1.49925: how many cents?
nTaxExact = nPriceCents * 7.5 / 100
? nl + "3. tax on " + showMoney(nPriceCents) + " at 7.5 % is 149.925 cents: a part of a cent does not exist"
? " rounded to the nearest cent: " + roundHalfUp(nTaxExact) + " cents = " + showMoney(roundHalfUp(nTaxExact))
# 4. "Half" cases: 2.5 rounds to 2 in banker's rounding (the .NET default) and to 3 in school rounding
oMath = new Math()
? nl + "4. rounding 0.5, 1.5, 2.5, 3.5:"
? " banker's (to even) : " + roundList(oMath,"ToEven")
? " school (away from 0): " + roundList(oMath,"AwayFromZero")
# 5. Splitting 100.00 into 3 shares must give back exactly 100.00: the extra cents go to the first shares
aShares = splitCents(10000,3)
? nl + "5. 100.00 split in 3: " + showMoney(aShares[1]) + ", " + showMoney(aShares[2]) + ", " + showMoney(aShares[3])
? " the shares add up to " + showMoney(aShares[1] + aShares[2] + aShares[3])
# 6. An invoice: lines in cents, the total, the tax on the total (not on every line!), the grand total
aLines = [["Notebook",1250,2],["Pen",199,10],["Backpack",4599,1]]
? nl + "6. An invoice:"
nSubtotal = 0
for aLine in aLines
nSubtotal += aLine[2] * aLine[3]
? " " + padRight(aLine[1],10) + padLeft(showMoney(aLine[2]),8) + " x " + aLine[3]
next
nTax = roundHalfUp(nSubtotal * 20 / 100)
? " subtotal " + showMoney(nSubtotal) + ", tax 20 % " + showMoney(nTax) + ", total " + showMoney(nSubtotal + nTax)
# "19.99" -> 1999
func toCents cText
return floor(number(cText) * 100 + 0.5)
# 1999 -> "19.99"
func showMoney nCents
cSign = ""
if nCents < 0
cSign = "-"
nCents = -nCents
ok
nUnits = floor(nCents / 100)
nRest = nCents - nUnits * 100
cRest = "" + nRest
if len(cRest) < 2
cRest = "0" + cRest
ok
return cSign + nUnits + "." + cRest
func roundHalfUp nValue
return floor(nValue + 0.5)
func roundList oMath, cMode
cText = ""
for nValue in [0.5,1.5,2.5,3.5]
nRounded = oMath.Round([ :value = nValue, :mode = cMode ])
cText += "" + nRounded + " "
next
return trim(cText)
# n cents in parts shares, the first shares get the remaining cents
func splitCents nCents, nParts
nShare = floor(nCents / nParts)
nRest = nCents - nShare * nParts
aList = []
for nI = 1 to nParts
if nI <= nRest
aList + (nShare + 1)
else
aList + nShare
ok
next
return aList
func padRight cText, nWidth
while len(cText) < nWidth
cText += " "
end
return cText
func padLeft cText, nWidth
while len(cText) < nWidth
cText = " " + cText
end
return cText
func yesNo lValue
if lValue
return "yes"
ok
return "no"1. 0.1 + 0.2 = 0.3 ? no
ten times 0.1 equals 1 ? no
2. three items of 19.99 = 59.97
3. tax on 19.99 at 7.5 % is 149.925 cents: a part of a cent does not exist
rounded to the nearest cent: 150 cents = 1.50
4. rounding 0.5, 1.5, 2.5, 3.5:
banker's (to even) : 0 2 2 4
school (away from 0): 1 2 3 4
5. 100.00 split in 3: 33.34, 33.33, 33.33
the shares add up to 100.00
6. An invoice:
Notebook 12.50 x 2
Pen 1.99 x 10
Backpack 45.99 x 1
subtotal 90.89, tax 20 % 18.18, total 109.07How it works#
Cents as integers#
Convert to whole cents when the amount comes in (toCents("19.99") → 1999), calculate with integers (they are exact), and turn them back to text only for display (showMoney). Three items of 19.99 are 5997 cents: no error is possible.
The .NET Decimal type is exact, but a decimal that crosses the bridge becomes a Ring number (a binary fraction again). In Ring, whole cents are the safe way. A database keeps money in integer columns for the same reason.
Rounding#
Percentages create parts of cents (7.5 % of 1999 is 149.925). You need a rule, and the rule must be written down:
- banker's rounding (
ToEven, the default ofMath.Round): half cases go to the even neighbor,0.5 → 0,1.5 → 2,2.5 → 2. Over many numbers it does not favor up or down. - school rounding (
AwayFromZero): halves go up,2.5 → 3. This is what most invoices and tax rules expect.
Math.Round takes the mode by name: oMath.Round([ :value = 2.5, :mode = "AwayFromZero" ]).
Splitting fairly#
100.00 divided by three is 33.333… splitCents gives each share floor(10000 / 3) = 3333 cents and hands the remaining cent to the first shares: 33.34, 33.33, 33.33. The sum is exactly 100.00. Never round every share separately — the shares might add up to 99.99 or 100.01.
Taxes#
The invoice calculates the tax once on the subtotal. Rounding the tax of each line separately gives a different total, and tax laws usually say which way is right. Always check the rules of your country.
What you learned#
- binary fractions are inexact,
- whole cents for calculation, text for display,
- rounding modes,
- fair splitting,
- tax on the total.
Exercises#
- Add a discount of 15 % to the invoice and decide when to round.
- Write
showMoneyIn(nCents, cCulture)usingCultureInfo(the tutorial Cultures). - Split 10.00 between 7 people and check the sum.