An invoice shows 92.18 in line-level VAT but 92.20 when VAT is computed on the document total. An installment plan adds up one cent short of the sale price. The total on screen does not match the total in the database. These gaps look tiny, but in accounting every cent is an open item that someone has to find. How do you prevent money and rounding errors in software? The answer is not a single trick. It is a handful of design decisions: the right data type, one rounding rule, rounding at the right moment, and lossless allocation of leftover cents.
In this guide we cover why floating point is wrong for money, how to store amounts, rounding modes, the line-based versus document-based VAT gap, the allocation algorithm, currency conversion, data transport, testing and payback. All figures are representative examples, not real market data. Which rounding and VAT method applies depends on the document type and on regulation; leave that decision to your financial advisor (certified accountant) and to Revenue Administration (GİB) rules.
Why Is Floating Point Wrong for Money?
Because floating-point numbers are stored in binary, and a simple decimal such as 0.1 is an infinite fraction in binary; the computer keeps an approximation. In money, an approximation is an error that becomes visible once it accumulates.
- 0.1 + 0.2: the result is 0.30000000000000004, which is not equal to 0.3.
- Adding ₺0.10 a thousand times: the total is 99.9999999999986, so the check "is it equal to 100?" fails. Of the 1,000 running totals, 984 do not match the expected value exactly.
- The rounding boundary: 1.005 is held as roughly 1.00499999999999989, so rounding to two places can give 1.00 instead of the expected 1.01.
Each error alone is tiny (on the order of 10⁻¹²). The real damage is broken equality checks and amounts sitting on a rounding boundary falling on the wrong side. So no layer should use floating point for money.
How Should Amounts Be Stored: Integer Cents or Fixed-Precision Decimals?
There are two safe routes: store the amount as an integer count of the smallest unit (cents), or use a decimal type with a fixed number of places. Both give identical results; the difference is convenience.
| Method | Strength | Weakness | Where it fits |
|---|---|---|---|
| Floating point | Fast, available everywhere | Approximate, accumulates | Nowhere for money |
| Integer cents | Exact addition and comparison | Division and percentages need their own rule | Calculation core, API |
| Fixed-precision decimal | Place count is in the type, readable | The right type must exist in your language and database | Database, business rules |
Scale is the second decision. Two places are enough for a document amount; unit prices, exchange rates and intermediate results need more. Example: 1,000 units × ₺0.0375 = ₺37.50. If the unit price is rounded to two places it becomes ₺0.04 and the amount becomes ₺40.00, a difference of ₺2.50 or 6.7%. A sensible layout: unit price and rate with 4 to 6 places, intermediate math at high precision, and rounding only at the boundary where the amount appears in a document. Every amount also carries a currency field.
The choice of database type belongs to the wider architecture; see our guide to choosing a database for the framework.
What Are the Rounding Modes and How Do They Change the Result?
Four common modes exist, and they differ only at the halfway point. For any value that is not exactly half, they all give the same answer, so the difference shows up only on amounts ending in ...5.
| Value | Half up | Half to even | Down (truncate) | Up |
|---|---|---|---|---|
| 0.125 (2 places) | 0.13 | 0.12 | 0.12 | 0.13 |
| 0.135 (2 places) | 0.14 | 0.14 | 0.13 | 0.14 |
| 2.5 (integer) | 3 | 2 | 2 | 3 |
| 3.5 (integer) | 4 | 4 | 3 | 4 |
| −2.5 (integer) | −3 | −2 | −2 | −3 |
Here "half up" rounds the halfway point away from zero and "down" rounds toward zero. "Half to even" (banker's rounding) sends the halfway point to the nearest even number; its purpose is to cancel directional bias. The effect shows in totals: take 500 lines of ₺0.125 and 500 lines of ₺0.135, so the true total is ₺130.00. Half up gives ₺135.00 (+₺5.00); half to even gives ₺130.00.
Which mode applies to official documents depends on regulation and contracts, so we give no rule. The software principle is: define the mode in one place, and route every calculation through the same rounding function.
When Should You Round: At Every Step or Only at the End?
Never in intermediate math; round only where a value is written into a document or record. The real debate begins with line-dependent and total-dependent calculations such as tax: line-based or document-based?
The representative seven-line invoice below uses a 20% VAT rate, rounded half up.
| Line | Qty × unit price | Net amount | VAT (exact) | Line VAT (rounded) |
|---|---|---|---|---|
| 1 | 8 × 5.09 | 40.72 | 8.144 | 8.14 |
| 2 | 3 × 2.84 | 8.52 | 1.704 | 1.70 |
| 3 | 1 × 25.47 | 25.47 | 5.094 | 5.09 |
| 4 | 1 × 9.87 | 9.87 | 1.974 | 1.97 |
| 5 | 1 × 21.25 | 21.25 | 4.250 | 4.25 |
| 6 | 9 × 26.11 | 234.99 | 46.998 | 47.00 |
| 7 | 8 × 15.02 | 120.16 | 24.032 | 24.03 |
| Total | 460.98 | 92.196 | 92.18 |
With the line-based method, total VAT is ₺92.18 and the grand total is 460.98 + 92.18 = ₺553.16. With the document-based method, VAT is computed once on the net total: 460.98 × 0.20 = 92.196, rounded to ₺92.20, and the grand total is ₺553.18. Same invoice, two totals, ₺0.02 apart.
Which method is correct can depend on the invoice type, the regulation and the counterparty's records. That is a regulatory question, not a software one; ask your financial advisor. The software's job is to apply the chosen method consistently on every document and to keep it in one configuration place. For document rules on the e-invoice (e-Fatura) side, see our e-invoice integration guide.
How Do You Allocate Leftover Cents Without Losing Any?
With the largest remainder method: floor each share to the cent, then hand out the remaining cents one by one, starting with the shares that have the largest fractional part. The total is always preserved. Plain rounding cannot do that.
| Case | Plain rounding | Largest remainder |
|---|---|---|
| ₺100.00 ÷ 3 | 33.33 + 33.33 + 33.33 = 99.99 (−0.01) | 33.33 + 33.33 + 33.34 = 100.00 |
| ₺50.00, weights 5:3:2:1 | 22.73 + 13.64 + 9.09 + 4.55 = 50.01 (+0.01) | 22.73 + 13.64 + 9.09 + 4.54 = 50.00 |
- Compute each share exactly (weight ÷ sum of weights × amount).
- Floor each share to the cent.
- Find the number of cents between the total and the sum of the parts.
- Add those cents one at a time, starting with the largest fractional part.
- If fractions tie, use a fixed order (the last line in our example) so the same input always gives the same output.
- Verify that the parts add up to the input.
In the second example, flooring already hit 50.00, while plain rounding pushed 4.5454 up to 4.55 and produced 50.01. Discounts in a promotion are spread the same way; the details are in our promotion and coupon engine guide.
Splitting installments is the same problem. ₺1,000 ÷ 3 installments: 333.33 + 333.33 + 333.34, with the last installment carrying the difference. With 12 installments, 83.33 × 11 = 916.63 and the last one is 83.37, a 4-cent gap. Alternatively, give the first four installments 83.34 and get 83.34 × 4 + 83.33 × 8 = 1,000.00. The chosen method should be written in the contract and applied to everyone alike.
In What Order Are Percentages, Discounts and VAT-Inclusive Prices Calculated?
The order must be fixed: discount, subtotal, tax, grand total. Change the order and both the result and the rounding point change, so keep the whole calculation in one function.
Working backwards from a VAT-inclusive price is a common source of errors. Do not round twice: compute the net amount and round it, then take VAT as gross minus net. If you compute and round both separately, the sum may miss the gross amount. Example: gross ₺0.03 at 20%. Net 0.025 rounds to 0.03, VAT 0.005 rounds to 0.01, and the sum is 0.04, not 0.03. Across the 399 gross amounts from ₺0.01 to ₺3.99, 67 of them (16.8%) fail to add back to the gross when both parts are rounded independently.
For layered pricing rules such as chained discounts, see our B2B price list guide.
What Should You Watch for in Currency Conversion and Currencies?
Conversion is a one-way operation; do not try to recover the original amount by converting back. Take a representative rate: 1 € = ₺41.37. ₺100.00 ÷ 41.37 = 2.4172 €, which rounds to 2.42 €. Convert that back and you get 2.42 × 41.37 = ₺100.1154, or ₺100.12. The round trip created a ₺0.12 difference. If it went the same direction on every transaction, 1,000 transactions would add up to ₺120.
The fix: record the source amount, the rate, the rate date and the result together on the converting transaction, and never derive the original amount later by converting back. Rate selection and period-end revaluation rules are in our multi-currency and exchange difference guide.
The number of decimals also depends on the currency: most use 2, some use 0, a few use 3. Do not hard-code "every amount has two decimals".
How Are Amounts Carried Through the Database, the API and the Browser?
The same principle applies in every layer: an amount must never turn into floating point. Three transport points stand out.
- Database: use a fixed-precision decimal type, with more places for unit prices (say 4) and 2 for document amounts. Totals can surprise you: if three lines are stored as 3.3350, SUM gives 10.0050, which rounds to 10.01; if the screen shows each line as 3.34 and adds 3 × 3.34 = 10.02, a 0.01 gap appears. The fix is to round and store the line amount when writing it to the document, and to produce the document total from the stored lines.
- API and JSON: do not carry amounts as numbers. In many environments 19.99 is read as 19.989999999999998. Send a string ("19.99") or integer cents (1999) together with a currency code, and write the format into the API contract.
- Browser: a live cart total is for display only. Send the lines to the server and let the server compute the final amount; if the two differ, the server wins and the screen refreshes.
Formatting is a separate trap. Turkish writes 1.234,50 and English writes 1,234.50; "1.234" is one thousand two hundred thirty-four in one place and 1.234 in another. Keep the stored form independent of locale, and apply locale only when displaying and parsing input.
How Do You Test and Measure Money Calculations?
Boundary tests check the decision points of rounding, while property-based tests check "the rule that must hold for every input" with random inputs.
| Test | Input | Expected |
|---|---|---|
| Halfway point | 0.005 and 0.025 | 0.01 and 0.03 (half up); a different mode may give 0.02 |
| Just below the boundary | 0.004999 | 0.00 |
| Negative amount | −0.005 | −0.01 (away from zero); the rule is written down |
| Tiny allocation | ₺0.01 ÷ 3 | Total 0.01, parts 0.00 / 0.00 / 0.01 |
| Property test | 10,000 random amounts and weights | Parts sum to the input; no part is negative; with equal weights the gap is at most 0.01 |
Alongside tests, track these measures in production:
- Total equality: the number of documents where the line total differs from the document total; the target is 0.
- Share of documents with rounding differences: for example, 180 of 10,000 documents is 1.8%; once the rule is fixed, the target is 0.
- Open reconciliation items: items that stay open because of a cent difference. The tolerance accepted for bulk payments is covered in our online payment reconciliation guide.
For the general testing framework, see our software testing guide.
What Is the Payback of Fixing Rounding Errors?
The direct gain comes from labor. Representative assumption: 12 rounding-related corrections a month at 45 minutes each, plus 6 hours a month spent hunting open items, at ₺300 per hour.
- Corrections: 12 × 45 min = 540 min = 9 hours × ₺300 = ₺2,700.
- Hunting open items: 6 hours × ₺300 = ₺1,800.
- Monthly gain: ₺2,700 + ₺1,800 = ₺4,500.
Say the investment (one calculation component, data migration, tests) is 12 person-days × ₺9,000 = ₺108,000. Payback: 108,000 ÷ 4,500 = 24 months. If the gain halves, it is 48 months. We did not count the problems avoided in audits and in customer relations; recompute with your own figures.
How Do You Run a Project to Prevent Money Calculation Errors?
Six steps work even on an existing system.
- Inventory: list every field that holds an amount and every place that does money math (cart, invoice, installments, reports, integrations).
- Scale: decide the type and number of places, remove floating point, and make the currency field mandatory.
- Rule: write down the rounding mode and timing; confirm the VAT method with your financial advisor.
- Allocation: write one allocation function for leftover cents and use it everywhere.
- Test: add boundary and property tests and turn past bugs into test cases.
- Monitoring: run line-versus-document total reconciliation regularly and wire any difference to an alert.
We recommend reading: What is subscription billing software? Proration and dunning guide
We recommend reading: What is custom software? Types, layers and cost guide
Custom Software with Money and Invoice Flows with İnovabil
At İnovabil our focus is building company-specific software from scratch: custom software, ERP, CRM, e-commerce, web, mobile and integration. For a system with amounts, invoices and payment flows, we recommend writing the calculation rules (rounding, allocation, VAT method) down during requirements, so the rule is defined in one place instead of spreading through the code.
We built CRM, purchasing and mobile for Avcılar İnşaat; a purchasing system for İZBAN; Kredili Kargo, a prepaid cargo system for a carrier; logistics and shipment planning plus wholesale order management for Karyak; and warehouse management plus a complaint and support management system for Sleepy. Send us a short summary of the money flow in your system; we will map the calculation points together and prepare a written assessment.
We recommend reading: Our software development process: step by step from analysis to launch
Frequently Asked Questions
Which data type should be used for money in software?
Not floating point. Use an integer count of the smallest unit, such as cents, or a decimal type with a fixed number of places. Both give exact results for addition and comparison. Store a currency field next to every amount.
Why does 0.1 + 0.2 not equal 0.3?
Floating-point numbers are stored in binary, and 0.1 is an infinite fraction in binary, so it is held as an approximation. The sum comes out as 0.30000000000000004. That is why money uses decimal or integer types.
What is banker's rounding, and should it always be used?
It is the mode that sends the halfway point to the nearest even number, which reduces directional bias. It need not be used everywhere; which mode applies to official documents depends on regulation and contracts, so consult your financial advisor.
Should VAT be computed per line or on the document total?
The same invoice can give different totals under the two methods; in our example the gap is ₺0.02. The valid method depends on the invoice type and regulation and should be confirmed with your financial advisor. The software applies the chosen method consistently.
How do you split ₺100 three ways without a cent difference?
Floor each share to the cent and give the leftover cent to the largest fractional part: 33.33 + 33.33 + 33.34. The total stays at 100.00; a fixed rule decides which line gets the extra cent when fractions tie.