Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

Stop Storing Money as a Float: Choose Integer or Decimal

Binary floats approximate many decimal fractions. Choose integer minor units or exact decimal storage based on currency scale, range, calculations, and rounding rules.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not use binary floating-point for money when exact decimal behavior matters. Store each amount with its currency, then choose integer minor units or an exact decimal type according to the operations, scale, and range your application requires. Storage, arithmetic, rounding, and display are separate decisions.

Why floating-point causes money problems

Binary floating-point stores numbers as finite binary approximations. Many familiar decimal fractions, including tenths, do not have finite binary representations, so a value such as 0.1 may be approximated. Arithmetic then operates on those approximations, which can make a calculated result differ slightly from the decimal value a user expects.

This is a representation issue, not a quirk of one language. JavaScript’s Number uses IEEE 754 double precision, and PostgreSQL 18 classifies real and double precision as inexact types. PostgreSQL explicitly warns that floating-point numbers should not be used to handle money because of potential rounding errors (PostgreSQL 18: Monetary Types).

Formatting a result to two decimal places does not undo errors accumulated during earlier calculations. For example, JavaScript’s toFixed() produces a formatted string; it does not change how the preceding arithmetic was represented or performed.

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

Choose a representation that fits the work

Approach Useful when Constraints to plan for
Integer minor units Amounts use a known currency subunit and addition or subtraction at that scale is central. Store the currency code and its scale; check integer range and overflow; define what happens to fractions smaller than the minor unit.
Exact decimal or database numeric Decimal inputs and calculations need configurable precision and scale. Set precision and scale intentionally, define rounding points, and consider performance and the target system’s semantics.
Database-specific money type The database’s currency-oriented type fits the application’s needs and assumptions. Verify its range, currency assumptions, conversions, locale behavior, and portability before adopting it.

Integer minor units

Represent a value in the smallest unit your application supports—for example, a currency’s cents—using an integer. Integer addition and subtraction at a fixed scale avoid fractional binary values, provided the integer remains within range and conversions are consistent.

Do not assume every currency has two fractional digits. Keep the currency identity explicit and obtain the applicable scale from a deliberate currency policy or reference. Also decide whether the system supports fractions of a minor unit for intermediate calculations, and guard against overflow. In JavaScript, integers are exactly represented only from −(253 − 1) through +(253 − 1); scaling an amount into minor units is safe only while it remains within that range (MDN: JavaScript Number).

Exact decimal or PostgreSQL numeric

Use an exact decimal representation when decimal fractions and configurable precision matter in calculations. PostgreSQL 18 describes numeric (also called decimal) as exact where possible and especially recommends it for monetary amounts and other quantities requiring exactness. Specify precision and scale in the schema rather than relying on defaults, and check the database’s documented input, cast, overflow, division, and rounding behavior for your chosen definition (PostgreSQL 18: Numeric Types).

Exact decimal storage is not cost-free: PostgreSQL notes that numeric calculations can be slower than integer or floating-point calculations. That trade-off is a reason to measure for your workload, not to substitute an inexact type where exact monetary behavior is a requirement.

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

Database-specific money types

A database-provided money type may suit a particular system, but its name alone does not establish that it matches your currency model. PostgreSQL 18 documents locale-sensitive output for its money type and cautions against converting floating-point values into money. Review range, formatting, conversions, currency assumptions, and portability before depending on it (PostgreSQL 18: Monetary Types).

Make rounding a business rule

Exact representation does not mean every calculation produces a value representable at the final settlement scale. Division, exchange or interest rates, tax calculations, and allocation among multiple recipients can yield more fractional digits than the currency’s minor unit permits.

Decide where rounding happens and which rule applies at each business boundary, such as tax calculation, splitting, or settlement. There is no universal rule established here: the appropriate policy depends on the application, contract, and jurisdiction. Apply it deliberately and consistently rather than letting a language conversion, database cast, or display formatter make the decision accidentally.

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

Keep currency and display separate from the amount

Persist every amount together with its currency. A bare scalar does not tell later code whether it means dollars, yen, cents, or another unit, and it cannot safely imply a universal number of decimal places.

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.

Keep storage and calculation values independent of presentation. Format for the intended locale and currency at the display or serialization boundary. PostgreSQL’s money output is locale-sensitive, so changing locale can change how a value is rendered; the formatted output is not a substitute for an explicit currency model.

A practical decision checklist

  • Identify the currencies you support and the scale used for each; do not hard-code two decimal places globally.
  • Choose integer minor units when fixed-scale integer operations and a sufficient range fit the application.
  • Choose an exact decimal type when decimal inputs or calculations need configurable precision, and specify precision and scale.
  • Define overflow handling, conversions, rounding, allocation, and settlement behavior as explicit rules.
  • Check the actual semantics of the database and runtime versions you deploy; PostgreSQL 18 guidance does not automatically describe other systems.
  • Format values for locale and currency only when presenting or serializing them.

The TC39 Decimal reference discusses the limits of binary floating-point, but labels itself a work in progress; it is a proposal, not evidence that a native Decimal type is a standard or broadly available JavaScript feature (TC39 Decimal proposal reference).

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.