Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A billing job converts long cents to float for a report: float f = cents;. Totals in the report differ from the database by a few cents for large accounts. Explain the cause.

Converting long to float is a widening conversion, so Java does it without a cast, but it is not exact: float has only a 24-bit significand, so integers above 2^24, about 16.7 million, cannot all be represented. A large account of 123,456,789 cents becomes 123,456,792 as a float, three cents off. The fix is to keep money as long cents or BigDecimal from end to end and only format it for display at the edge, and to have review or static analysis rules flag lossy conversions of monetary values.

Why this question is asked

Money bugs are expensive even when they are small, because a report that disagrees with the database by a few cents destroys trust in the whole system and triggers manual reconciliation. This one is particularly confusing because the code has no cast, and Java normally forces a cast wherever data might be lost. Interviewers ask it to see whether we understand the floating point format well enough to explain exactly which values break and why. In this answer, we will recap how float stores numbers, reproduce the difference, see why only large accounts are affected, look at the same problem with double, and then fix the report and prevent the pattern.

1A Quick Recap of How float Stores Numbers

A float is 32 bits in the IEEE 754 format: 1 sign bit, 8 exponent bits and 23 stored significand bits, plus one implicit leading bit, giving 24 bits of precision. The significand holds the digits of the number and the exponent says where the binary point goes. This lets float cover a huge range, up to about 3.4 x 10^38, but with only about seven decimal digits of precision at any magnitude.

The consequence for integers is simple. Every integer up to 2^24 = 16,777,216 fits exactly in 24 bits. Above that, the float can only represent every second integer, then every fourth, and so on, because the spacing between neighbouring floats doubles each time the exponent grows.

2Why the Compiler Allows It

Java calls long to float a widening primitive conversion, because the range of float is far larger than the range of long. Widening conversions are allowed without a cast. The language specification acknowledges that widening from int or long to float, and from long to double, may lose precision, but it still classifies them as widening because the magnitude is never lost. So float f = cents compiles silently, and nothing in the code warns us.

3Reproducing the Difference

We can see the rounding with a few account balances in cents.

Java
long small = 1_234_567L; // 12,345.67 in cents
long boundary = 16_777_217L; // 2^24 + 1
long large = 123_456_789L; // 1,234,567.89 in cents
System.out.println((long) (float) small); // 1234567: exact, below 2^24
System.out.println((long) (float) boundary); // 16777216: off by 1 cent
System.out.println((long) (float) large); // 123456792: off by 3 cents
Gap between neighbouring float values
Magnitude of the valueGap between floatsError in cents, at most
up to 2^24 (16,777,216)10
2^24 to 2^2521
2^25 to 2^2642
2^26 to 2^27 (about 1.3 million dollars)84
2^30 (about 10.7 million dollars)12864
The error per conversion is up to half the gap, and it grows with the balance.

Between 2^26 and 2^27, which covers 123,456,789, neighbouring floats are 8 apart, so the value is rounded to the nearest multiple of 8, which is 123,456,792. That matches the symptom exactly: small accounts are always correct and large accounts are off by a few cents. If the report also sums floats, each addition rounds again, so the error can grow with the number of lines.

4Double Has the Same Problem, Later

Switching the report to double moves the boundary but does not remove it. double has a 53-bit significand, so it represents every integer exactly up to 2^53, which is 9,007,199,254,740,992. Long cents above that would be rounded too, and long to double is also a silent widening conversion. In practice, the bigger danger with double is not individual balances but arithmetic: amounts such as 0.1 have no exact binary form, so adding and multiplying decimal amounts as doubles produces small errors that show up in rounding.

5Fixing the Report and Preventing It

The fix is to stop converting. The database stores exact cents, and the report should keep them exact all the way through: sum them as long, or as BigDecimal if we need division or tax calculations, and only turn them into text at the very end.

Java
// Before: lossy
float f = cents;
row.setAmount(f);
// After: exact cents, formatted only for display
long totalCents = lines.stream().mapToLong(Line::cents).sum(); // Math.addExact if overflow is possible
BigDecimal amount = BigDecimal.valueOf(totalCents, 2); // 1234567.89, scale 2
String shown = NumberFormat.getCurrencyInstance(Locale.US).format(amount); // $1,234,567.89

BigDecimal.valueOf(unscaled, scale) creates the exact decimal value from cents without passing through floating point. NumberFormat accepts a BigDecimal, so formatting does not reintroduce the error.

To prevent the pattern, we make lossy conversions on money visible. Code review guidelines should say that monetary values never become float or double. Static analysis can help: custom rules (for example in Error Prone, PMD or an ArchUnit test) can flag float and double fields or variables with names such as amount, price or balance, and flag implicit long to float conversions. A reconciliation test that compares report totals with database totals for very large values would also have caught this before production.

6Follow-up questions

Why is long to float called widening if it loses information?

Because widening in Java is about range, not precision. Every long value is within the range of float, so the magnitude is always preserved, but the low-order digits may be rounded. The specification lists int to float, long to float and long to double as widening conversions that can lose precision.

Would int cents have the same issue?

Yes. int to float is also a silent widening conversion and loses precision above 2^24. int to double is always exact, because every int fits in 53 bits, but int cents overflow at about 21.4 million dollars, which is another reason to prefer long.

Is it ever acceptable to show money as double?

For a rough chart of trends, a double may be fine because nobody reconciles a chart. For any number a person compares with another system, such as a report, an invoice or a statement, we keep exact values and format them at the edge.

7Conclusion

In this answer, we saw that long to float compiles because Java treats it as widening, but float's 24-bit significand cannot represent every integer above 2^24, so large balances in cents are rounded to the nearest representable float, three cents off for 123,456,789. double moves the limit to 2^53 but does not remove it. The fix is to keep money as long cents or BigDecimal end to end, format only at the edge, and make lossy conversions of monetary values a review and static analysis rule.

What to say in the interviewAbout 90 seconds aloud

Open with

The cause is that long to float is a widening conversion in Java, so it compiles without a cast, but it can lose precision. A float has a 24-bit significand, which means it can represent every integer exactly only up to 2 to the 24, about 16.7 million.

Then, if they want more

Above that the gap between neighbouring floats grows: 2, then 4, then 8 and so on. So a balance of 123,456,789 cents, about 1.2 million dollars, is rounded to the nearest multiple of 8 and becomes 123,456,792, three cents off. Small accounts are below the limit and are exact, which is why only large accounts disagree.

If the report then sums floats, every addition rounds again. Converting to double would move the limit to 2 to the 53, but long to double is also lossy above that, and doubles still cannot represent decimal fractions exactly, so it is not the real fix.

The real fix is to keep money exact end to end: long cents, summed with addExact if overflow is possible, or BigDecimal when we need division or tax, and only format to text at the edge, for example BigDecimal.valueOf(cents, 2) and a currency NumberFormat.

To prevent it, I would make it a review rule that money never becomes float or double, back it with a static analysis rule on monetary names, and add a reconciliation test with very large balances.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A billing job converts long cents to float for a report: float f = cents;. Totals in the...": https://scrivio-interview-library.netlify.app/java/types-and-values/8deb3159ce/