Imagine a checkout page that shows a cart total of €19.99. The API recalculates it and saves 19.98. The confirmation email shows €19.99 again, and the Stripe charge matches neither, because somebody multiplied by 100 in the browser. Nothing crashed, and every test with round numbers passed. The money just went through four layers, and each layer had its own idea of what a number is.
Money bugs rarely come from one bad line. They come from a missing contract between the database, the API, and the UI.
A money contract that holds comes down to a few decisions:
- Store amounts exactly, with the currency next to them.
- Pick one wire format for JSON and document it.
- Never assume two decimal places. Look up the currency's exponent.
- Round once, on the server, with a named rounding mode.
- Use formatting only for display, and parse user input deliberately.
- Let the server compute every total, including the amount you send to the payment provider.
The rest of this article follows one amount from a PostgreSQL column to a React screen and back to Stripe.
Why floats are the wrong tool for money
A JavaScript number is a 64-bit IEEE 754 double. MDN's Number reference puts its precision at about 15 to 17 significant decimal digits, and arithmetic beyond that is rounded. The bigger problem for money is that most decimal fractions have no exact binary form.
You can reproduce the usual surprises in any browser console:
0.1 + 0.2returns0.30000000000000004.Math.round(1.005 * 100)returns100, not101, because1.005 * 100is actually100.49999999999999.(2.55).toFixed(1)returns'2.5'. MDN's toFixed page explains that the closest representable double is slightly below 2.55. Note thattoFixedalso returns a string.
A native decimal type would fix this, but the TC39 Decimal proposal is still an early-stage proposal; its README lists Stage 1 as of November 2026. It isn't something you can ship on.
Floats are fine for a chart axis or a percentage bar. They are the wrong tool anywhere a cent has to match another system.
Pick a column type: numeric, integer minor units, or money
PostgreSQL gives you three realistic options.
Option 1: numeric with an explicit scale. The PostgreSQL numeric documentation says numeric is "especially recommended for storing monetary amounts." Calculations are exact for addition, subtraction, and multiplication. Declare the precision and scale yourself, for example numeric(19,4), so the database rejects values with more decimals than you intend to keep.
Option 2: integer minor units. Store 1999 for €19.99 in an integer or bigint column, next to the currency code. The dinero.js storage guide recommends storing the amount as an integer in minor units, with the currency code and exponent. Integer math is exact and fast. The trade-off is that every reader of the column has to know the exponent, and integer division truncates.
Option 3: the money type. Most teams should avoid it. The PostgreSQL money documentation says its fractional precision depends on the database's lc_monetary setting. Loading data into a database with a different setting might not work, and dividing by an integer truncates. It also has no currency attached, so one column can't safely hold euros and yen.
Whichever you pick, store the currency as an ISO 4217 code in its own column. A CHECK constraint or a reference table keeps typos like EUD out of the data. An amount without its currency is only half a value.
The amount arrives as a string, and that is a feature
This is where many full-stack apps break. You declare amount: number in TypeScript, and at runtime the value is a string.
The node-postgres types documentation says the driver converts a database type to a JavaScript string when it has no parser for that type. The pg-types README says int8 (bigint) results come back as strings on purpose, because a large value would overflow a JavaScript number. In practice, numeric columns also arrive as strings by default in node-postgres, which is the same rule at work. Check your driver version and configuration, because any code that registers a type parser can change it.
ORMs inherit or wrap this behavior:
- Prisma: the special fields and types page says
Decimalfields are represented by decimal.js. decimal.js'stoJSONreturns a string, so aDecimalin a NestJS response becomes a JSON string. In a quick test, a decimal.js value of 12.50 serialized as"12.5", dropping the trailing zero. Prisma's defaultDecimalmapping on PostgreSQL isdecimal(65,30), per the schema reference. Set precision and scale explicitly with a native type attribute. - TypeORM: the entities documentation says
bigintmaps to a string. Decimal columns on PostgreSQL usually arrive as strings too, because that is what the driver returns. Verify this in your setup. A columntransformerlets you convert explicitly.
The string is a feature, not a bug. The driver is refusing to round your money silently. The bug comes later, when code treats the value as a number:
"10.00" + "5.00"returns"10.005.00".- A global parser that turns numeric into
parseFloatmakes the error go away, and puts floating-point math back into every amount.
Convert deliberately instead. Keep decimal strings as strings at the API boundary, or parse them into a decimal library or integer minor units in one place. Add a test that checks the runtime type of an amount coming out of your repository layer, not just the TypeScript type.
BigInt columns and JSON
If you store minor units in bigint and your ORM hands you JavaScript BigInt values, the next crash happens in serialization. MDN's BigInt page states that JSON.stringify() raises a TypeError for any BigInt unless a toJSON method is defined. In Node, the error reads "Do not know how to serialize a BigInt."
You have two reasonable fixes:
- Convert at the response boundary, for example in a NestJS interceptor or a response DTO, to a string or to a number you have range-checked.
- Use a plain
integercolumn when your amounts fit. As a cent amount in a JavaScript number,Number.MAX_SAFE_INTEGER(9007199254740991) is about 90 trillion in a two-decimal currency.
Avoid patching BigInt.prototype.toJSON globally unless your whole team agrees on it. It changes behavior for every library in the process.
Choose a wire format and write it down
The JSON contract matters more than the column type, because it's the part both teams read. Three formats are well established:
- Integer minor units plus currency:
{ "amount": 1999, "currency": "EUR" }. This is the model the Stripe currencies page uses: 1099 means 10.99 USD, and 10 means 10 JPY. It's simple to validate, but every client has to know each currency's exponent. - Decimal string plus currency:
{ "amount": "19.99", "currency": "EUR" }. It's readable in logs and survives any JSON parser without precision loss. Clients must not do arithmetic on it with plain numbers. - Units plus nanos plus currency: the Google API Money type splits whole units and nano units (10^-9), each with a range, plus an ISO 4217 currency code. It's precise and explicit, but heavier than most apps need.
You will also see a decimal as a JSON number, as in Zalando's API guidelines (rule 173). That works when every consumer parses decimals exactly. A browser's JSON.parse turns it into a double, so React apps usually prefer one of the first two formats.
Pick one format, put it in your OpenAPI schema, and use it for every amount in the API. Mixing 19.99 in one endpoint with 1999 in another is how a price ends up 100 times too large.
Not every currency has two decimals
Hard-coding * 100 works until your first customer pays in yen. The ISO 4217 list, maintained by SIX Group, assigns each currency a number of minor units:
- 0 decimals: JPY and ISK.
- 2 decimals: USD and EUR.
- 3 decimals: BHD, KWD, JOD, OMR, and TND.
- 4 decimals: CLF, a unit of account.
Payment providers don't always follow ISO exactly. As of November 2026, Stripe's currencies page says:
- ISK must be sent as a two-decimal value ending in
00, even though ISO lists zero decimals. - UGX follows the same two-decimal pattern.
- HUF and TWD payouts must be divisible by 100.
- Currency codes in requests go in lowercase.
Some providers add rules for three-decimal currencies too. Checkout.com's amount guide, for example, documents that the last digit must be 0.
Keep a small currency table on the server with two exponents per currency: the ISO exponent for display, and the provider exponent for payment requests. Generate both from current documentation instead of memory, and check the provider's page when you add a currency.
Round once, in one place
Most cent differences come from rounding the same value in two places, with two different rules. The defaults are not consistent across your stack:
- PostgreSQL numeric: rounds ties away from zero. Floating-point types round ties to even on most machines, per the numeric documentation.
- Intl.NumberFormat: defaults to
halfExpand(ties away from zero) and also offershalfEven, per its MDN constructor page. - decimal.js: defaults to ROUND_HALF_UP, per its API docs.
So 2.5 rounded to a whole number is 3 in a numeric column, 2 in a float column, and 2 in the browser if someone set roundingMode: "halfEven".
For most apps, a few rules fix this:
- Do business rounding on the server only, with the mode written down next to the code.
- Store rounded results, and never re-round a stored amount.
- Let the browser format values it receives, never recalculate them.
Taxes add a second source of differences: whether VAT is rounded per line or on the subtotal. Peppol BIS Billing 3.0 is a widely used e-invoicing profile of the EU standard EN 16931. It rounds line net amounts and document totals to two decimals, and calculates VAT on the subtotal for each VAT rate, not per line. Summing per-line VAT can be one cent off that result. If you issue e-invoices, match your calculation to the profile you must follow. National rules for receipts can differ, so check them for the countries you sell in.
Displaying amounts in React with Intl.NumberFormat
Format at the very edge, in the component that renders text. Intl.NumberFormat does the locale work for you:
de-DEwith EUR renders 1234.5 as "1.234,50 €".en-USwith EUR renders it as "€1,234.50".ja-JPwith JPY renders it as "¥1,235", with no decimals, because the default fraction digits come from the ISO 4217 minor units.
A few options are worth knowing:
currencyDisplayswitches between a symbol, a narrow symbol, a code, or the currency's name.currencySign: "accounting"shows negative values in parentheses.roundingModesets how the display rounds. Display rounding should rarely matter, because the server has already rounded.
Create formatters once, for example in a useMemo or a module-level cache keyed by locale and currency, rather than on every render. Keep the formatted string out of state and API payloads. It's a display artifact, and locale output can change between browser versions, so don't assert exact formatted strings in tests across different environments.
Parsing what users type
Intl.NumberFormat formats numbers. It has no parse method, and the obvious shortcuts fail quietly on decimal-comma input:
parseFloat("1.234,56")returns1.234, a wrong number with no error.Number("1.234,56")returnsNaN.
For an amount field, decide which input you accept and normalize it explicitly:
- Get the locale's separators from
formatToParts. Forde-DE, the group part is "." and the decimal part is ",". - Strip group separators, replace the locale's decimal separator with a dot, and reject anything left that isn't a digit, a minus sign, or a single dot.
- Reject more decimals than the currency allows instead of silently rounding them.
- Send the normalized decimal string or minor-unit integer to the API, never the raw text.
Validate again on the server. The client parser improves the user experience; the server check is what protects the data. For accessibility, keep a visible label with the currency and use inputmode="decimal" so mobile keyboards show a decimal key.
Taxes: VAT-inclusive in the EU, usually before tax in the US
Where a price includes tax is a product decision with legal edges, so store the net amount, the tax rate, and the tax amount separately. That lets you render each market correctly. This section is an overview, not legal advice.
- EU consumers: the Consumer Rights Directive, Article 6(1)(e) as adopted, requires informing consumers of the total price "inclusive of taxes". The Price Indication Directive (98/6/EC, Article 2) defines the selling price as the final price. B2C prices in the EU are therefore shown with VAT included. Check the current consolidated text on EUR-Lex and national rules before you launch.
- US: product prices are commonly shown before sales tax, which varies by state and locality. The FTC's rule on unfair or deceptive fees requires an upfront total price, but it covers live-event tickets and short-term lodging, and it allows government charges to be disclosed separately before payment, per the FTC FAQ.
Have the API return the amounts the UI needs (net, tax, and gross) instead of letting React calculate VAT from a rate.
The server owns the total, including the Stripe amount
A total calculated in the browser is a suggestion. The server should recompute every order total from its own prices, quantities, discounts, and tax rules before charging anything.
The order of operations should look like this:
- The client sends product IDs, quantities, and the currency, not prices or totals.
- The server loads prices, computes line amounts and tax with its rounding rules, and stores the result.
- The server converts the final amount to the provider's minor unit using the currency table, then creates the payment with that integer.
- The client displays the amounts the server returns.
If the client must show a total before checkout, fetch a quote from the server, or treat the client value as an estimate. If you want to guard against stale screens, have the client echo the quoted total and reject the request on a mismatch, never charge the client's number. Pair this with an idempotency key on the payment request so a retried submit can't charge twice.
Libraries: when they help
You don't need a library to follow this contract, but a good one removes whole classes of mistakes. Versions here are as of November 2026:
- dinero.js: models money as an amount, a currency, and a scale. v2 became stable with the 2.0.0 release in March 2026, per its releases page. Many older tutorials still show the v1 or v2 alpha API, so check which version an example uses.
- decimal.js: arbitrary-precision decimal arithmetic, and the library behind Prisma's
Decimal. - big.js: a smaller decimal library for when you only need basic arithmetic and rounding.
Pick one decimal representation per codebase. A mix of dinero objects, decimal.js values, and raw numbers recreates the original problem.
Checklist
- Store amounts in
numericwith explicit precision and scale, or as integer minor units, with an ISO 4217 currency column. Avoid themoneytype for multi-currency apps. - Expect strings from node-postgres and ORMs, and convert them in one place. Don't install a global float parser.
- Convert
BigIntbefore JSON serialization. - Document one wire format for every amount in the API.
- Keep a server-side currency table with ISO and provider exponents. Never hard-code
* 100. - Round on the server only, with a named rounding mode, and decide between per-line and per-rate VAT.
- Format with
Intl.NumberFormatat render time, and parse input explicitly with the locale's separators. - Show EU consumers VAT-inclusive prices, and return net, tax, and gross from the API.
- Let the server compute the total and the payment amount, and use an idempotency key on the charge.



