Nov 24, 2026
Your totals are off by a paisa — don't store money as floats
0.1 + 0.2 is 0.30000000000000004, and that error compounds across every line of an invoice. Store money as integer minor units and only format it at the edge.
The invoice total is off by one paisa. The line items add up to ৳1,234.56, the total says ৳1,234.55, and nobody can find the missing paisa because there is no bug in the arithmetic. The arithmetic is doing exactly what it was told. It was told to do money math with floats.
Open a console and type the most famous line in JavaScript:
0.1 + 0.2; // 0.30000000000000004
That is not a JavaScript bug. JavaScript numbers are IEEE 754 double-precision floats — binary fractions — and most everyday decimals have no exact binary form. 0.1 is stored as the closest binary value that exists, which is very slightly off. One error that small is harmless. The trouble is that the errors pile up, and that rounding looks at the exact stored value, not the one you meant.
The ones that actually bite
0.1 + 0.2 is a party trick. The dangerous cases are the ones that look correct. Sum a thousand items at ten paisa each as floats and you get 99.9999999999986 instead of 100 — and total === 100 is false, so any check that compares the total to what it should be fails for no visible reason.
The worst one is the conversion everyone writes first:
Math.floor(1.15 * 100); // 114, not 115
1.15 * 100 is 114.99999999999999, so flooring it drops a whole paisa. Somebody types ৳1.15 and your system quietly works with ৳1.14. And toFixed(2) at the end does not save you either: 1.005 is stored as 1.00499999999999989, so rounding it to two places gives 1.00 when a person doing it by hand would write 1.01.
On a dashboard this shows up exactly where it hurts most — the totals. A single line item is off by a fraction of a paisa and nobody notices. A thousand lines, a discount, and a tax calculation later, the invoice total disagrees with the sum of its own lines by one paisa, and the support ticket says "your math is wrong" with no error in any log, because nothing failed.
The fix: integers, not floats
The fix is old and boring, which is why it works. Keep money as an integer count of the smallest unit — paisa, cents — and never let a float near the arithmetic. ৳19.99 becomes 1999. Integer addition and subtraction are exact in JavaScript up to Number.MAX_SAFE_INTEGER (2^53 − 1, about 9 quadrillion), which is far beyond any invoice you will ever total.
const pricePaisa = 1999;
const qty = 3;
const subtotalPaisa = pricePaisa * qty; // 5997, exact
The trick is getting from the user's input to that integer without ever building the float 19.99. Parse the string, not the number. Split on the decimal point and treat each side as an integer:
function toMinorUnits(input: string): number | null {
const m = /^(\d+)(?:\.(\d{1,2}))?$/.exec(input.trim());
if (!m) return null; // not a plain amount — show an error, don't guess
const whole = Number(m[1]);
const frac = Number((m[2] ?? "").padEnd(2, "0"));
const minor = whole * 100 + frac;
return Number.isSafeInteger(minor) ? minor : null;
}
toMinorUnits("19.99"); // 1999
toMinorUnits("1.5"); // 150
toMinorUnits("1.155"); // null — three decimals is an error, not a guess
Read the value from input.value, which is always a string, rather than input.valueAsNumber, which has already turned it into a float. Math.round(parseFloat(input) * 100) reintroduces exactly the error you were avoiding — that is the most common way floats sneak back in.
Formatting is the last step, and it is the only place a float is allowed to appear. Divide at the edge, for display only:
const formatBDT = (paisa: number) =>
new Intl.NumberFormat("en-BD", {
style: "currency",
currency: "BDT",
}).format(paisa / 100);
formatBDT(1999); // ৳19.99
Percentages have the same problem
A percentage is a decimal too, so store it the same way — as integer basis points, hundredths of a percent. 15% is 1500, 12.5% is 1250, 100% is 10000. Then multiply first and divide last, keeping the divisor a power of ten you control:
const subtotal = 1999 * 3; // 5997 paisa, exact
const vatBps = 1500; // 15%
const vat = Math.round((subtotal * vatBps) / 10000); // 900 paisa
const total = subtotal + vat; // 6897
And round once, at a place you chose, and write the rule in a comment. There is no neutral rounding — half-up, half-even, and always-up all give different answers on ties, and the choice belongs in the calculation, not in the UI formatting. Do not round after every intermediate step unless the real-world rule says to; rounding a per-day figure and then summing it compounds the error the same way the floats did.
The database has to agree
None of this holds if the column undoes it. In PostgreSQL, store money as an integer column in minor units, or as NUMERIC/DECIMAL with an explicit scale — never as float or double precision. A float column stores the same approximation the JavaScript number did, and now the error lives in the one place every report reads from.
This is also why payment APIs work the way they do. Stripe takes amounts in the smallest currency unit — cents, paisa — as integers. If your dashboard stores ৳19.99 as a float and converts it to paisa at the moment you charge, you have built the Math.floor(1.15 * 100) bug into your checkout. Store the integer from the start and the conversion is a value you already have.
What you give up
Minor units differ by currency, and hard-coding * 100 will charge a Japanese customer a hundred times too much — the yen has zero decimal places — or a Kuwaiti one ten times too little, since the dinar has three. Store the currency with every amount, in the database and in every JSON payload, and derive the scale from it instead of assuming two decimals. Intl already knows the answer for each currency.
Integers also have a ceiling. Past Number.MAX_SAFE_INTEGER, addition stops being exact. For an invoice total that limit is absurd — about 9 quadrillion paisa — but if you genuinely need arbitrary-precision decimals, reach for a decimal library like decimal.js or big.js, and pass it strings, not floats. Decimal(1.005) faithfully copies the already-wrong float; Decimal("1.005") is exact.
And do not confuse "the display rounds correctly" with "the math is correct." 3 * 19.99 prints 59.97, which looks fine. The stored value is not exactly 59.97 — JavaScript prints the shortest decimal that round-trips to the same double, which hides the error until another operation exposes it. The display was never the problem. The representation was.
Floats are fine for physics and graphics. For money, store integers, choose one rounding rule on purpose, and test against ties. The 0.30000000000000004 joke is funny right up until it is your customer's total.