Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Don’t use binary floating-point for values that must represent money exactly. Store each amount with its currency, then choose either integer minor units or an exact decimal type based on your calculations, range, and rounding requirements. Neither choice removes the need to define how fractional results are rounded.

Why floating-point can misrepresent money

Binary floating-point stores numbers as finite binary approximations. Many familiar decimal fractions, including tenths, do not have finite binary representations, so a value entered as a decimal may be stored as a nearby value. Arithmetic then operates on that approximation; errors can accumulate or make mathematically equivalent calculations produce different results.

This is a property of the representation, not a defect unique to one language. JavaScript’s Number uses IEEE 754 double precision, and PostgreSQL 18 classifies real and double precision as inexact types. PostgreSQL explicitly cautions: “Floating point numbers should not be used to handle money due to the potential for rounding errors.” PostgreSQL 18: Monetary Types

Formatting a result to two decimal places does not repair calculations that already used approximations. For example, JavaScript’s toFixed() produces a formatted string; it does not change how earlier arithmetic was performed or make the underlying value exact.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a representation that matches the work

Representation Good fit Key constraints
Integer minor units Amounts use a known currency subunit, and addition or subtraction at that scale is central. Keep the currency code and its scale explicit. Check integer range, overflow, and how fractional minor-unit results are handled.
Exact decimal / numeric Decimal inputs or database calculations need configurable precision and scale. Choose precision and scale deliberately; define rounding for results that do not fit the settlement scale. Performance and behavior vary by system.
Database-specific money type A database’s own type fits its currency and fixed-fraction assumptions. Verify range, conversion rules, currency assumptions, locale-sensitive output, and portability before relying on it.

Use integer minor units when the scale is fixed

Represent an amount as an integer count of the relevant subunit—for example, cents for a currency whose unit is divided into hundredths. This makes addition and subtraction exact at that scale, provided the integer range is sufficient and conversions are consistent.

Do not assume every currency has two fractional digits. Store the currency identity and apply the appropriate scale rather than letting an unlabeled integer imply dollars, cents, or any other unit. Also decide what to do when a calculation produces a fraction of a minor unit; integer storage does not determine the rounding rule.

Rank #2
Sale
The Psychology of Money: Timeless lessons on wealth, greed, and happiness
  • Ideal for Gifting
  • Ideal for a bookworm
  • Compact for travelling

In JavaScript, scaled values must remain within the safe-integer range for exact integer representation: from −(253 − 1) through +(253 − 1). Larger values require a different strategy, such as an appropriate arbitrary-precision integer implementation. MDN: Number.MAX_SAFE_INTEGER

Use exact decimal or numeric when decimal calculations matter

PostgreSQL 18’s numeric (also called decimal) supports configured precision and scale. PostgreSQL describes it as exact where possible and says it is especially recommended for monetary amounts and other quantities where exactness is required. Its calculations can be slower than integer or floating-point calculations, so consider workload as well as correctness. PostgreSQL 18: Numeric Types

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Specify the intended precision and scale in the schema instead of relying on defaults. Then verify how the actual database and application handle input, casts, overflow, division, and rounding. PostgreSQL’s guidance applies to PostgreSQL 18; another database or language may have different type semantics.

Treat a database money type as a system-specific choice

A money type may look convenient, but its behavior is tied to the database’s assumptions. In PostgreSQL 18, money output is locale-sensitive, and the documentation advises against converting floating-point values to money. Check what currency and fractional precision the type represents, how values convert, and whether those rules suit your application before choosing it. PostgreSQL 18: Monetary Types

Keep currency, rounding, and display explicit

Store currency alongside the amount

An amount without a currency is ambiguous. Persist the currency identifier with the value, and make the scale used for integer minor units explicit. Do not build a system around an unlabeled scalar or a universal assumption of two decimal places.

Define rounding at business boundaries

Exact decimal storage does not guarantee that every result is representable at the final settlement scale. Division, exchange rates, taxes, and splitting a bill can produce additional fractional digits. Decide where rounding occurs and which rule applies at each business boundary, based on the relevant contract, policy, and jurisdiction. There is no single rounding rule established for every application.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Sale
I Will Teach You to Be Rich: No Guilt. No Excuses. Just a 6-Week Program That Works (Second Edition)
  • It can be a gift option
  • Comes with secure packaging
  • Helpful in various ways

For allocations, also define how any remainder is assigned so that the total distributed amount agrees with the amount being split. Keep that policy separate from the choice of numeric type.

Format for the intended locale only at presentation

Keep the stored value and its currency separate from its displayed form. Apply the intended currency symbol, grouping, decimal separator, and locale when presenting or serializing the amount. In PostgreSQL, the money type’s output is locale-sensitive, which is another reason not to treat formatted output as a portable stored representation. PostgreSQL 18: Monetary Types

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

A practical decision checklist

  • Currency and scale: Is the unit scale known and explicit for every supported currency?
  • Operations: Do you mainly add and subtract fixed-scale amounts, or do decimal calculations need configurable precision?
  • Range: Can amounts and intermediate results exceed the selected integer or decimal limits?
  • Rounding: Where do rates, taxes, division, allocation, and settlement require a defined rule?
  • System behavior: Have you checked the target database and runtime’s input, arithmetic, cast, overflow, and rounding semantics?
  • Performance and portability: Does the representation fit the workload and remain understandable across the systems that exchange the values?
  • Display: Is locale-aware formatting kept at the presentation boundary rather than used as storage?

For PostgreSQL 18, numeric is the documented choice when exact monetary calculations are required. Integer minor units are a sound alternative when the currency scale is explicit and the range is safe. In either case, currency, rounding, and formatting remain separate decisions.

Quick Recap

SaleBestseller No. 1
SaleBestseller No. 2
The Psychology of Money: Timeless lessons on wealth, greed, and happiness
The Psychology of Money: Timeless lessons on wealth, greed, and happiness
Ideal for Gifting; Ideal for a bookworm; Compact for travelling
$10.99
SaleBestseller No. 5
I Will Teach You to Be Rich: No Guilt. No Excuses. Just a 6-Week Program That Works (Second Edition)
I Will Teach You to Be Rich: No Guilt. No Excuses. Just a 6-Week Program That Works (Second Edition)
It can be a gift option; Comes with secure packaging; Helpful in various ways
$9.15

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.