Skip to content

Rounding Errors in Online Stores: Why Prices Go Wrong in Code and How Developers Prevent Them

Java logo above the question What Does += Mean in Java.

A customer emails to say their invoice total is one cent different from the checkout page. Your accountant finds that monthly sales reports never quite match the payment provider. A discount of 10 percent produces a price ending in .9999. These small mismatches are common in custom-built stores and booking systems, and they usually come from the same cause: how code adds up money.

This guide explains the problem for business owners. It starts with the Java operator +=, which developers use to build running totals, and explains why computers struggle with some decimal numbers. It then covers the methods developers use to keep money exact, how tax rounding complicates things, and the tests to ask for.

What += means

In Java and many other languages, += adds a value to a variable and stores the result in the same variable. total += price; is shorthand for total = total + price;. It is how code builds a running total, for example adding each line of a shopping cart to the order total, or each day's sales to a monthly figure.

Java's version has one quiet detail: it converts the result back to the variable's type automatically. If total is a whole-number type and the price has decimals, total += price compiles without complaint and silently drops the fraction. A careless choice of type can therefore lose money without any error message.

Why computers get decimals wrong

Most programming languages store decimal numbers in a binary floating point format, called double in Java. It is fast and fine for measurements, but it cannot represent many everyday decimals exactly. The value 0.1, for example, is stored as a number very slightly different from one tenth.

The classic demonstration is that adding 0.1 and 0.2 in many languages gives 0.30000000000000004 rather than 0.3. One tiny error is invisible, but a running total that adds thousands of prices with += can drift by a cent or more, and comparisons such as "is the total exactly 30.00?" can fail unexpectedly.

Floating point dollarstotal += 0.10total += 0.20total += 0.300.6000000000000001Whole centstotal += 10total += 20total += 3060 cents, shown as $0.60
Storing money as whole cents, or as exact decimals, keeps running totals exact.

How developers keep money exact

There are two standard approaches. The first is to store amounts as whole numbers of the smallest currency unit, such as cents, and only convert to dollars for display. Many payment providers, including Stripe, take amounts in the smallest currency unit for exactly this reason. Adding whole numbers with += is always exact.

The second is to use an exact decimal type. In Java that is BigDecimal, which stores numbers in decimal form and lets developers choose exactly how and when to round. Databases have equivalent types, such as DECIMAL or NUMERIC columns, which should be used for prices and totals rather than floating point columns.

Rounding rules are a business decision

Even with exact arithmetic, someone has to decide how to round. Should 2.345 become 2.35 or 2.34? The common rule is to round half up. Some financial systems use round half to even, sometimes called banker's rounding, which rounds 2.345 to 2.34 and 2.355 to 2.36 to avoid bias over many transactions. Neither is wrong, but your system, your invoices, and your accounting software should agree.

Tax adds another layer. Sales tax or VAT can be calculated per item and then added up, or calculated once on the order total, and the results can differ by a cent. Rules vary by country and state, so agree on the method with your accountant and make sure the developer implements exactly that.

Discounts, currencies, and refunds

Percentage discounts, multi-currency prices, and partial refunds are where rounding problems usually appear. A 15 percent discount split across three items may not add up to 15 percent of the total. Converting currencies can produce long decimals. Refunding one item from a discounted order raises the question of how much of the discount to take back.

Write down the rules for each case, with worked examples, before development starts. If you use a platform such as Shopify or WooCommerce, these rules are mostly built in, so check its settings rather than rebuilding them. Our guide to starting a Shopify store covers the setup.

Tests to ask for

  • A cart with many low-priced items, checking the total to the cent.
  • Percentage discounts on odd amounts, compared with your accountant's figures.
  • Tax calculated on a sample of real orders, matched against your invoices.
  • Partial refunds and currency conversions, with expected results written in advance.

Automated tests that check these cases on every update stop a fix in one place from quietly breaking totals somewhere else.

Reconciling with your reports

Compare your store's sales report with your payment provider's payouts and your accounting software every month. Small, consistent differences usually point to rounding or tax method mismatches; larger ones point to missing orders or refunds. A spreadsheet that lines up the three sources makes this quick, and our guide to VLOOKUP in Google Sheets shows how to match orders by ID.

Platforms versus custom code

Store platforms such as Shopify and WooCommerce handle money arithmetic for you and have been tested on millions of orders. Rounding problems are far more common in custom code: custom booking systems, quote calculators, subscription billing, and integrations that copy orders between systems. If you commission any of these, ask specifically how amounts are stored and rounded. Our guide to WooCommerce covers a platform that handles this out of the box, and our guide to off-by-one errors describes another small bug that custom code often introduces.

Totals you can trust

The += operator builds running totals in Java, and it is exact only when the numbers are. Floating point types cannot store many decimals precisely, so money should be stored as whole cents or as exact decimals such as BigDecimal. Rounding and tax rules should be agreed in writing and tested on real examples.

If your store or booking system shows totals that do not match, our web development team can trace the cause. For a broader view of store platforms, see our guide to running a Shopify store.

Want this checked on your own site?

Check one URL in Swiss Knife SEO without an account. A free account supports crawls of up to 100 URLs.