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

Should you use a float for money? Usually not as the authoritative representation of an amount that must be stored or calculated exactly. Binary floating-point cannot exactly represent many decimal fractions, so values that look simple in decimal can acquire small binary errors. Use decimal arithmetic or scaled integers where appropriate, and decide explicitly how and when to round. Python, JavaScript and PostgreSQL each have different tools and limits.

Why binary floating-point is risky for monetary values

Most computers represent floating-point numbers in binary. Many decimal fractions have no finite binary representation, just as one third has no finite decimal representation. A value such as 0.1 may therefore be stored as a nearby binary value rather than exactly as one tenth. Arithmetic on those approximations can produce results that display unexpectedly or fail exact comparisons.

This does not make floating-point useless. It is effective for many approximate measurements. The problem is treating it as the authoritative representation of a monetary amount when exact decimal storage or controlled rounding is required. PostgreSQL describes its real and double precision types as inexact, and MDN documents JavaScript Number as IEEE 754 double-precision binary floating point.

Keep four separate decisions in view: how input is represented, what precision arithmetic uses, when and how a result is quantized (rounded to a chosen scale), and how the result is formatted for display. Choosing a decimal type addresses representation; it does not, by itself, choose your business rounding policy.

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

Choose an approach based on your calculations

Approach Best fit Key constraint
Integer minor units Amounts with a fixed, known scale, such as storing a two-decimal amount as whole hundredths Scale and currency must be explicit; fractional rates and different scales require additional handling
Decimal arithmetic Decimal amounts and calculations involving fractional intermediate values Precision, rounding mode and quantization point still need deliberate configuration
Binary floating-point Approximate measurements where tiny representation differences are acceptable Not an exact decimal representation for many monetary fractions

Before choosing, check the needed input and storage exactness, whether intermediate rates or fractions occur, currencies’ scales, value range, rounding rules, API serialization, database portability, and operational simplicity. Integer minor units are straightforward for fixed scales, while decimal arithmetic is more natural when calculations involve decimal fractions or varying scales.

Python: construct Decimal from text and control its context

Python’s Decimal type can preserve decimal input when constructed from a string. Start with Decimal("19.99"), not Decimal(19.99). The second expression first creates a binary float and then converts that float’s exact approximation, which can yield a long decimal expansion. Python’s official Decimal documentation states, “Decimal numbers can be represented exactly.”

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

price = Decimal("19.99")
quantity = Decimal("3")

with localcontext() as ctx:
    ctx.prec = 28
    ctx.rounding = ROUND_HALF_EVEN
    subtotal = price * quantity

cent = Decimal("0.01")
posted_amount = subtotal.quantize(cent)

This example makes the input decimal, arithmetic context and final quantization visible. The chosen precision and rounding mode are illustrative, not universal recommendations. Set context deliberately: precision, rounding and traps affect arithmetic behavior. Quantize at the point your domain requires a fixed scale; do not assume every intermediate value should be rounded to two places. A rate or allocation may need more precision until a later posting or settlement step.

JavaScript: know Number’s boundary and select a representation

JavaScript’s Number is IEEE 754 binary64. Even a numeric literal that looks like an integer has type Number. Its exact-integer range is from −(253−1) through +(253−1); beyond that range, adjacent integers cannot all be represented exactly. MDN describes the format as having a 53-bit significand.

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

Use scaled BigInt for fixed-scale amounts

For a fixed scale, an amount can be stored as an integer count of minor units using BigInt. For example, a two-decimal amount of 19.99 can be represented as 1999n. Carry the currency and scale alongside the integer, validate the allowed range and inputs, and implement any conversion or rounding rule explicitly. BigInt avoids Number‘s integer precision ceiling, but it does not decide how fractional calculations should be rounded. JavaScript also does not implicitly mix BigInt and Number; convert deliberately rather than combining them in arithmetic.

Use decimal arithmetic for fractional intermediates

When calculations involve rates, fractional intermediate values, or multiple scales, a decimal arithmetic library is often a more suitable model than repeatedly converting to minor units. Select and review a maintained library for the application’s requirements, validate inputs, and define precision and rounding policy explicitly. The TC39 Decimal proposal repository describes the problem area and proposal context; it is not evidence that JavaScript currently has a built-in Decimal type.

PostgreSQL: prefer numeric for exact decimal storage

For exact decimal storage and calculations, PostgreSQL recommends numeric (also called decimal). Choose a precision and scale that cover the domain, for example numeric(12, 2) for a domain that permits at most twelve total digits with two fractional digits. That example is not a universal limit: the appropriate declaration depends on the values the application must support. PostgreSQL says numeric calculations are exact where possible, while noting that they may be slower than integer or floating-point arithmetic.

CREATE TABLE invoices (
    id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    amount numeric(12, 2) NOT NULL
);

INSERT INTO invoices (amount) VALUES (19.99);

Do not pass a floating-point value into an otherwise exact numeric column and assume the original decimal input can be recovered. Preserve decimal input through the application and database boundary instead of first converting it to a binary float.

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

Why money is not always the portable choice

PostgreSQL’s money type stores an amount at fixed fractional precision determined by the lc_monetary setting, and its output is locale-sensitive. That coupling can complicate portability and formatting across environments. For applications that need explicit decimal scale and predictable data handling, PostgreSQL’s documentation advises using numeric for exact monetary storage and calculations.

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

Make rounding a documented domain rule

No single rounding mode or currency scale is universal across every business requirement and jurisdiction. Choose the applicable rule with the people responsible for the domain, document it, and apply it consistently. Language defaults are not a substitute for that decision. Python exposes context and rounding configuration; PostgreSQL’s numeric type provides decimal arithmetic but does not establish every application’s policy.

  • Choose the scale: identify the scale required for the stored or posted amount rather than assuming every currency or operation uses two decimal places.
  • Choose the point: decide whether rounding occurs at each operation, at line-item or invoice totals, or at another defined boundary.
  • Choose the mode: name the rounding behavior in configuration or code instead of inheriting a default silently.
  • Test boundaries: include values exactly at, just below and just above rounding boundaries, plus maximum supported amounts and fractional-rate calculations.
  • Keep display separate: formatting controls how an amount is shown; it should not silently become the storage or arithmetic policy.

Practical implementation checklist

  1. Define the domain: list supported currencies, scales, maximum values, rate calculations, and the required rounding rule.
  2. Preserve input: parse decimal text directly into Python Decimal or a suitable JavaScript decimal representation; do not route an exact amount through a float first.
  3. Choose the storage model: use integer minor units for fixed-scale cases when their range and operations fit, decimal arithmetic for decimal fractions and varying scales, and PostgreSQL numeric(p,s) for exact database decimal values.
  4. Set precision and rounding: configure arithmetic context or application logic, and quantize only at the explicitly chosen business boundary.
  5. Validate boundaries: check numeric ranges, scales, conversions, serialization behavior and rounding cases at each system interface.
  6. Format only at the edge: produce locale-appropriate display text from the authoritative amount without treating formatted strings as the underlying value.

As reference bounds, MDN’s JavaScript Number documentation gives the exact-integer range as −(253−1) through +(253−1). The PostgreSQL 15 Numeric Types documentation states that numeric supports up to 131072 digits before and 16383 digits after the decimal point. Those are technical type properties, not application recommendations.

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.

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