Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 38 ยท Payment Concepts and Security

Money in Code: Amounts and Currencies

Learn why money must be stored as integers, how Stripe's minor units work, zero-decimal and three-decimal currencies, safe rounding for tax and splits, and formatting with Intl.

If you remember one rule from this course, make it this: never store money as a floating-point number. The first time you compute 0.1 + 0.2 in JavaScript you get 0.30000000000000004, and at scale those tiny errors become lost cents and accounting disputes. Stripe avoids the problem by using integers in the smallest currency unit for every amount. This lesson shows how that works, including the exceptions, and how to do arithmetic safely.

Minor units

For US dollars the smallest unit is the cent, so $20.00 is 2000. For euros it is the cent too. Stripe calls this the minor unit and the amount you send is always an integer count of them.

Price Currency amount you send
$20.00 usd 2000
EUR 9.99 eur 999
GBP 0.50 gbp 50

Zero-decimal and three-decimal currencies

Not every currency has two decimal places. Some have none: the Japanese yen has no smaller unit in practice, so amount: 500 with currency: "jpy" means 500 yen, not 5.00. A few currencies have three decimals (for example the Kuwaiti dinar), and Stripe requires those amounts to be a multiple of ten for them. Always look up the currency instead of assuming 100.

Stripe's documentation lists the zero-decimal currencies (such as JPY, KRW, VND and others). In code, keep a small table and convert with a helper. The standard library helps: Intl.NumberFormat knows how many fraction digits each currency uses.

Node.js Find a currency's decimal places

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Now a pair of helpers to move between friendly decimal amounts and Stripe's integers. Note the use of rounding after multiplying, because 19.99 * 100 is 1998.9999999999998.

Node.js Convert to and from minor units

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Never accept a price from the browser. A customer can edit it. Look up the amount on your server from your own product data, or better, from a Stripe Price, and only trust the product ID and quantity that the client sends.

Adding, splitting and rounding

Integers make addition exact. The tricky parts are percentages (tax, discounts, fees) and splitting (three people share a bill). Each multiplication by a fraction can produce fractional cents that you must round, and the rounding rules must be consistent, usually round half up, applied at the same place every time.

The most important idea in splitting is never lose or invent a cent. If you split 1000 into three, plain rounding gives 333 + 333 + 333 = 999. The leftover cent has to go somewhere. A reliable method is to divide and give the remainder to the first parts:

Node.js Split an amount without losing cents

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Tax works the same way: compute per line, round, and add up, or compute on the total, but pick one rule and keep it. In Europe and the US the law (and your tax tool) often specifies which. Stripe Tax, covered later, handles the rounding for you.

Formatting for people

Only convert to a decimal string when displaying. Intl.NumberFormat formats currency for the viewer's locale, including symbol placement and separators.

Node.js Format money for different locales

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Minimum and maximum charge amounts

Stripe enforces a minimum charge per currency (roughly the equivalent of fifty US cents, with differences per currency), because below that fees would exceed the amount. There is also a maximum (eight digits in minor units). Your code should validate before calling Stripe so the customer sees a friendly message rather than an API error.

A checklist

  • Store and transmit integers in minor units. Convert only at the edges.
  • Look up decimal places per currency; never hard-code 100.
  • Round exactly once, in a documented place, and make parts sum to the whole.
  • Compute prices on the server.
  • Keep the currency next to every amount; an amount without a currency is meaningless.

Next: PCI DSS and Card Data Safety.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A payment of 500 yen is created. What amount do you send?

    JPY is a zero-decimal currency, so the amount is the number of yen.
  2. 2. Why is Math.round(19.99 * 100) used instead of 19.99 * 100?

    Binary floating point cannot represent most decimals exactly; rounding after multiplication recovers the integer.
  3. 3. You split 1000 cents between 3 people. What is the safest result?

    The parts must sum to the whole, so the leftover cent goes to one of them.
  4. 4. Where should the price of an order come from?

    Anything from the browser can be edited. Look up prices on the server.

Summary

  • Money is an integer in the minor unit; use Math.round after any multiplication.
  • Currencies have 0, 2 or 3 decimals; ask Intl.NumberFormat instead of assuming 2.
  • When splitting or allocating, make the parts sum exactly to the total.
  • Format for display only, with the viewer's locale.
  • Never trust prices from the client.

Next: PCI DSS and Card Data Safety