Is converting money to integers for calculations a reasonable API design?

November 12, 2026 4 answers
Asked by neon_turtleneck

My API handles balances and other financial amounts from a database and external sources. The decimal precision is known, and integer arithmetic looks easier to maintain than using BigDecimal throughout.

Would it make sense to convert incoming amounts to integer minor units, calculate with those, and convert back for responses?

Answers

Quibbish

Yes, storing amounts in minor units is a reasonable approach, especially for balances and addition or subtraction. For a currency with two decimal places, 12.34 becomes 1234 minor units. Make the unit and currency explicit so nobody accidentally mixes dollars with cents.

In Java, a long gives you a much larger range than an int, though you should still check for overflow. For output, BigDecimal.valueOf(minorUnits).movePointLeft(2) is safer than inserting a decimal point into a string, particularly for negative or small values.

t0.clk:

That conversion assumes two decimal places. The scale needs to come from the currency or whatever precision the amount represents.

Reply to this answer
Foop

Integers don't get rid of rounding decisions. Division can produce fractions of a minor unit. For example, 75 cents divided by 2 is 37.5 cents, so you need a rule for the remainder rather than silently truncating it.

Multiplication by rates needs care too. You can use a higher intermediate scale or decimal arithmetic, then round at a defined point using the rule required for that calculation.

Reply to this answer
unusually round meeting

Watch the assumption that every currency has cents. Japanese yen has no fractional minor unit, while the Iraqi dinar has three decimal places. An integer amount without its currency and scale is pretty easy to misinterpret when it crosses an API boundary.

Reply to this answer
alu.4v

For more involved calculations, such as amortization, keeping everything at the final currency scale can round away useful precision at each step. BigDecimal with extra intermediate precision may be easier to reason about there, even if the stored balance is an integer.

Whichever representation you choose, decide where rounding happens and which mode applies. Converting to integers doesn't make those choices disappear.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions