We found a weird bug in Java. Guess it's from the people who removed the German date format:
Locale.setDefault(Locale.ENGLISH);
System.out.println(Locale.getDefault());
System.out.println(NumberFormat.getInstance().format(0.001));
Locale.setDefault(new Locale("cs", "CZ"));
System.out.println(Locale.getDefault());
System.out.println(NumberFormat.getInstance().format(0.001));
Take a moment to guess what this code should return. Now take a look at what it returns:
en
0.001
cs_CZ
0
Yes, "0.001" for English and "0" (instead of "0,001") for Czech. Why would that be? The following code gives a hint:
for (final Locale locale : Locale.getAvailableLocales()) {
final int maximumFractionDigits = NumberFormat.getInstance(locale).getMaximumFractionDigits();
if (maximumFractionDigits != 3) {
System.out.println(locale.getDisplayCountry() + " -> " + maximumFractionDigits);
}
}
It returns:
Czech Republic -> 2
Lithuania -> 2
So for some reason maximumFractionDigits
is locale specific. Neither we nor the our representative Czechs know why.
All frustration about this random behavior aside: which other fields of the NumberFormat
are also locale specific, i.e. have to be initialized each time we need a NumberFormat
?
(After spending an entire work day trying to figure this bug out, I'm inclined to ask "why", but that's probably opinion based either way.)
PS. we ran the program with different Java versions. For some reason it seems to work in update 51 and 112, but is broken in update 45, 131, 144, 152 and 161. Java 9 seems to be completely funky.