|
Download calculation/money.md from swedishembedded/knowledge: direct link, hf CLI and curl.
- Browser
- Download file 4.29 kB
-
https://huggingface.co/swedishembedded/knowledge/resolve/main/calculation/money.md
- Command line
-
hf download hf://swedishembedded/knowledge/calculation/money.md
-
curl -L -o money.md https://huggingface.co/swedishembedded/knowledge/resolve/main/calculation/money.md
4.29 kB
| # SPDX-License-Identifier: CC-BY-4.0 | |
| # Copyright (c) 2026 Martin Schröder <info@swedishembedded.com> | |
| title: Money - exact arithmetic, rounding, tax, interest | |
| tiers: rule 1, procedure 2, example 3, mistakes 2 | |
| # Money: exact arithmetic, rounding, tax, interest | |
| For computing an amount of money: a tax line, a penalty, interest, a total. | |
| <!-- tier: 1 --> | |
| ## Rule | |
| **The task's own files and stated rules always win over this page; use this page only for what | |
| the task leaves open.** | |
| 1. **Use the task's formula, rounding and units as written.** If the request says how an amount | |
| is computed and rounded, that sentence is the method; do not replace it with another. | |
| 2. **Take input amounts as they stand.** A number in a task file is in the unit that file or the | |
| request states. Never rescale, convert or "correct" an input because it looks unusual. | |
| 3. **A rate given in percent is divided by 100 exactly once.** A table value of 25 means a | |
| factor of 0.25. A value already written as 0.25 is not divided again. | |
| 4. **Compute exactly.** Use a decimal type built from text, or whole cents as integers. Binary | |
| floating point stores most decimal fractions slightly off, so a value that should end in | |
| exactly half a cent can round the wrong way. | |
| 5. **"Round half up"** means a value exactly halfway goes up. Many programming languages round | |
| half to even by default; that is a different rule. | |
| 6. **Round where the task says**, not earlier and not later. Do not round where it says nothing | |
| and the arithmetic is exact. | |
| 7. **Net and gross.** Tax on a net amount = net x rate. From a gross amount: net = gross / (1 + | |
| rate), tax = gross - net. Tax is never gross x rate. | |
| 8. **Keep the sign.** "A minus B" is negative when B is larger. | |
| <!-- tier: 2 --> | |
| ## Procedure | |
| 1. Copy the task's sentence about computing and rounding into your notes. Underline: the | |
| formula, the rounding step, the unit. | |
| 2. Find the rate in the task's own table, for this item's category and date. Convert percent to | |
| a factor once. | |
| 3. Compute each line in a script with exact decimals, in the order the task's formula gives. | |
| 4. Round at the step the task names. Keep the rounded line. | |
| 5. Add lines with the script. For a derived figure (A minus B) use the totals and keep the | |
| sign. | |
| 6. Interest for days, when the task asks for it: amount x yearly rate x days / the year length | |
| the task states; round once at the end. | |
| 7. Sanity check one line by estimate: is it the right order of magnitude? A tax line a hundred | |
| times too large or too small means a rate or unit slip in your script, not in the task's | |
| data. | |
| <!-- tier: 3 --> | |
| ## Worked example | |
| Illustration only, with invented data. Your task's data differs: take no fact, count or | |
| expected result from this example. | |
| **Percent once.** A table gives the rate as 6 (percent). Net 33.25: tax = 33.25 x 6 / 100 = | |
| 1.995 exactly; with "round half up to 0.01" that is 2.00. | |
| ```python | |
| from decimal import ROUND_HALF_UP, Decimal | |
| def money(x): | |
| return x.quantize(Decimal("0.01"), ROUND_HALF_UP) | |
| rate = Decimal("6") / 100 # percent to factor, once | |
| print(money(Decimal("33.25") * rate)) # 2.00 | |
| print(round(33.25 * 0.06, 2)) # 1.99, wrong: binary float | |
| ``` | |
| **Gross to net.** Gross 199.00 at 6 percent, with "net rounded half up, tax = gross - net": | |
| net = 199.00 / 1.06 = 187.7358..., rounded 187.74; tax = 199.00 - 187.74 = 11.26. | |
| (199.00 x 0.06 = 11.94 would be wrong.) | |
| **Totals and sign.** Tax on sales 13.26, tax on purchases 40.00: payable = 13.26 - 40.00 = | |
| -26.74. The minus sign stays. | |
| **Estimate.** 6 percent of about 200 is about 12. A script that prints 1194.00 or 0.12 for | |
| that line has a slip in the rate; the input amount is not the problem. | |
| <!-- tier: 2 --> | |
| ## Common mistakes | |
| - Following a habit or another document's method where the task states its own formula or | |
| rounding. | |
| - Deciding that the task's amounts must be in another unit and rescaling them by a hundred. | |
| - Dividing a percent rate by 100 twice, or not at all. | |
| - Using floating point and `round()`, and landing one cent off on a half-cent value. | |
| - Computing tax on a gross amount as gross x rate. | |
| - Rounding too early or too late against the stated rule. | |
| - Dropping the minus sign of a negative difference. | |