Rulesnorthwind-amount-as-float

northwind-amount-as-float

warning

northwind/correctness/amount-as-float

Docs page ↗

The amount handed to charges.create comes from a float expression (a price in dollars, a tax multiplier, a division) instead of an integer number of cents.

21

codebases affected, of 58

36%

of everything we scanned

▼14

points since the first docs fix

1

docs commits aimed at this

Where it’s failing

amount is a non-integer value — Northwind expects an integer number of minor units.

what we find

what we change it to

const total = subtotal * 1.0825 // 42.674999999999997
await northwind.charges.create({
amount: total, // dollars, and a float
currency: 'usd',
customer,
})
const totalCents = Math.round(subtotalCents * 1.0825) // 4267
await northwind.charges.create({
amount: totalCents, // integer minor units
currency: 'usd',
customer,
})

Keep money in integer minor units end to end and round once at the boundary: Math.round(dollars * 100) for input, Math.round(cents * rate) for derived amounts. Never let a float reach charges.create.

Metadata

Category
correctness
Type
suggestion
Severity
warning
Recommended
yes
Languages
javascriptpython

Live telemetry

21 / 58 scans

Fired in 36% of scanned codebases.

After every docs commit

docs(api): minor-units callout on the charges reference

4af07c2 · merged 6 weeks ago · PR by your team, scored by api-doctor

19% → 15%▼4pts

A fix for your docs page

Wherever this rule fires, a page on your docs site taught it. api-doctor writes the edit and opens it as a pull request on your docs repo — you review and merge.

No fix yet

Draft a docs fix for this rule

api-doctor writes the docs change from the fire pattern — you review it

History

Active

Flag Math.round(x * 100) as a float path, not a fix

3 weeks ago · via GitHub

d709c34
Superseded

Track amounts across destructured order objects

9 weeks ago · via GitHub

a5f18b2