← Nesim Apps

Java to Kotlin Multiplatform: Number Formatting Traps

By Nesim Razon, independent mobile developer · 2026-11-28

#Dev notes#Android#iOS

Moving an Android app to Kotlin Multiplatform looks easy when it comes to showing numbers: you only need a few decimals and a thousands separator. Then you find that the two Java tools you relied on are not available in common code, and that they never rounded the same way in the first place. This is what I learned while moving the formatting of an existing app, and how I rebuilt it so that Android and iOS print exactly the old text.

What disappears in common code

Common Kotlin code is compiled for every target, so it cannot call Java classes. That removes java.text.NumberFormat, which my app used for money amounts with grouping separators, and String.format, which it used for everything with a fixed number of decimals. Each platform has its own formatting classes, and you could call them through a small interface with one implementation for Android and one for iOS.

I did not take that route. Two platform implementations mean two rounding behaviours, two sets of locale rules and a result that depends on the operating system version. The app shows calculated values to people, and a difference in the last digit between their old phone and their new one is exactly the kind of thing that destroys trust. I wanted one function in shared code with one behaviour.

Trap one: NumberFormat rounds the exact binary value

A double cannot store most decimal fractions exactly. What it stores is the closest binary fraction, which may be a hair above or a hair below the number you typed. NumberFormat, with its default half-even mode, looks at that exact stored value when it decides which way to go.

Take 0.125. This one can be stored exactly in binary, so it really is halfway between 0.12 and 0.13. Half-even then picks the neighbour whose last digit is even, and the result is 0.12, shown as 0,12 in a Turkish locale. With no decimals at all, 2.5 becomes 2, not 3. Many people expect school rounding here and are surprised.

When a value is not exactly halfway, the tie rule never applies. The stored value is simply above or below the middle, and it is rounded to the nearer side, even if the text you typed looks like a perfect tie.

Trap two: %.1f rounds half up on the short decimal

String.format with %.1f behaves differently. In effect, Java works from the shortest decimal text that identifies the double and then rounds that text half up. The value 1.15 is stored a little below one point one five. The exact stored value would therefore round down to 1.1, but %.1f sees the short form 1.15, treats it as a tie and rounds up to 1.2, shown as 1,2.

So the same app, on the same phone, had two rounding rules. Money went through one and percentages and ratios through the other. Nobody had noticed because each screen was consistent with itself. If you replace both with a single helper that multiplies by ten, rounds and divides, you silently change the output of at least one of them.

The small details that also change output

Rebuilding both rules with integer arithmetic

I wanted a result that does not depend on how any platform converts a double to text. So the new code never builds a decimal string from the double. It reads the raw bits instead, which are identical on every target, and splits them into the mantissa and the exponent.

The mantissa is multiplied by ten, one hundred or one thousand, depending on the number of decimals. A mantissa has at most 53 bits, so the product still fits in a Long. The exponent then tells how far to shift right. The bits that fall off are the remainder, and comparing the remainder with half of the shifted unit says whether the value is above, below or exactly in the middle.

From there the two behaviours are two flags. Half-even rounds a true tie to the even neighbour. The Java style used by %.1f first rounds true ties up, and then performs one extra check: if the value is just below the middle, but is the double closest to that middle point, it is treated as a tie as well. That single check reproduces the 1.15 to 1,2 case without ever producing a string.

Proving that nothing changed

Rewriting rounding code by reading documentation is not enough. Before touching anything, I ran the old Android code over a large set of inputs and saved its output, about 22 thousand lines of it. The new shared code has to reproduce that file exactly. I described this approach in an earlier post on this site about keeping old code as a test oracle.

On top of the saved output there is a permanent test that runs on the JVM, where the old Java calls still exist. It feeds very many random inputs to both versions, the Java expression and my function, and requires identical text. Random inputs matter because the interesting cases are rare: a hand-written list would contain the ties I already knew about and none of the others.

Those comparisons run on the JVM only. On iOS the shared code is compiled, not compared. What carries the result over is that the function does integer arithmetic on the raw bits and never asks the platform to format a number.

When a platform formatter is the better choice

This does not mean you should always write your own formatter. If you need full localisation for many languages, with their digit shapes, currency positions and plural rules, use the platform classes. They know far more about locales than a small helper ever will.

My case was narrow: one locale for numbers, a fixed set of formats and a strong requirement to keep the previous output. Under those conditions a few dozen lines of integer arithmetic are easier to trust than two platform libraries. Decide which of the two situations you are in before writing any code.

A third rule hiding in the calculations

Formatting was not the only place where Java decided the digits. The calculation code rounded intermediate results with BigDecimal: first to six decimals, then to two, both times half up. BigDecimal built from a double holds the exact binary value, so multiplying the double by a million and calling a rounding function is not the same operation. The product itself is rounded before you ever see it.

The shared replacement splits the double into mantissa and exponent, multiplies the mantissa by a million using two Long halves so that nothing is lost, and only then applies the two rounding steps. A JVM test compares it with the old BigDecimal expression on random inputs. The lesson is to look for rounding in the logic as well as on the screen, because the two can use different rules and both must survive the move.

A checklist before you port formatting code

Frequently asked questions

Why does 0.125 round to 0.12 in Java NumberFormat?

0.125 is stored exactly, so it is a true tie, and the default half-even mode chooses the neighbour with the even last digit.

Why does String.format give 1.2 for 1.15?

It works from the short decimal form 1.15 and rounds half up, although the stored double is slightly below that value.

Is String.format available in Kotlin Multiplatform common code?

No. It is a JVM feature, so common code needs its own formatting or a platform-specific implementation.

Should every KMP app write its own number formatter?

No. For full localisation use the platform classes. A custom one makes sense when you need one exact, narrow behaviour on all targets.

More from the blog