Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Why can String.format("%.2f", 1234.5) print "1234,50" on some servers, and how do you make output stable?

String.format without a Locale argument uses the JVM's default locale, and many locales, such as German, French or Italian, write the decimal separator as a comma, so the same code prints 1234.50 on one server and 1234,50 on another. To make output stable, we pass a locale explicitly: Locale.ROOT, or another fixed locale, for anything machine-readable such as CSV, JSON or logs that tools parse, and the user's locale for text people read. The same trap affects other locale-sensitive methods such as toUpperCase and toLowerCase, where the Turkish locale maps i and I differently.

Why this question is asked

Bugs like this are maddening because the code is correct on the developer's laptop and wrong on one server, often a new region or a container image built with a different language setting. A CSV file with 1234,50 in a comma-separated column breaks every row after it, and a JSON number with a comma is invalid. Interviewers ask this to see whether we know where Java's default locale comes from and how to make formatting independent of it. In this answer, we will reproduce the problem, see where the default comes from, fix it for machine-readable and user-facing output, and look at the same issue in case conversion, including the Turkish i.

1A Quick Recap: Locale-Sensitive Formatting

A Locale is Java's description of a language and region, such as en-US or de-DE. Many formatting operations depend on it: which character separates decimals, which groups thousands, how dates are written, and even how letters change case. When a method has no Locale parameter, or we call the overload without it, Java uses the default locale of the JVM.

String.format("%.2f", x) is such a method. It is shorthand for String.format(Locale.getDefault(Locale.Category.FORMAT), "%.2f", x).

2Reproducing the Problem

We can see the difference by passing the locales explicitly, which is what the default does behind our back.

Java
double total = 1234.5;
System.out.println(String.format(Locale.US, "%.2f", total)); // 1234.50
System.out.println(String.format(Locale.GERMANY, "%.2f", total)); // 1234,50
System.out.println(String.format(Locale.FRANCE, "%.2f", total)); // 1234,50
System.out.println(String.format("%.2f", total)); // depends on the server
System.out.println(Double.toString(total)); // 1234.5 on every server

Notice the last line. Double.toString and String.valueOf are not locale-sensitive; they always use a dot. The problem is specific to formatting APIs that take a locale, such as String.format, formatted(), Formatter, NumberFormat and DecimalFormat.

3Where the Default Locale Comes From

The JVM sets its default locale at startup from the operating system environment: on Linux the LANG and LC_ALL variables, on Windows the user's regional settings. It can be overridden with the system properties user.language and user.country, or user.language.format for the formatting category, or changed at run time with Locale.setDefault, which affects the whole JVM.

That is why the bug appears per machine. A container image built on a base image with LANG=de_DE.UTF-8, a server provisioned in another region, or a CI agent with different settings all change the result of code that never mentions a locale.

Step throughHow String.format("%.2f", x) picks its separator
  1. Locale passed explicitly?
    YesUse that localeNoUse the JVM default FORMAT locale
  2. Default set by -Duser.language or Locale.setDefault?
    YesUse that settingNoUse the OS settings (LANG, LC_ALL, Windows region)
  3. Format the number with that locale's symbolsdot in en-US, comma in de-DE

3 steps in order. Play them, or step through them one at a time.

Every step after the first depends on the environment, which is why output differs per server.

4Fix: Choose the Locale on Purpose

The fix is to decide, for each piece of output, who reads it. Machine-readable output, such as CSV files, JSON, configuration, protocol messages and log lines that tools parse, needs one fixed format everywhere. Locale.ROOT is the neutral locale meant for this, and it uses a dot for decimals and no grouping in %f.

Java
// Machine-readable: identical on every server
String csvCell = String.format(Locale.ROOT, "%.2f", total); // 1234.50
String notFixed = "%.2f".formatted(total); // NOT fixed: formatted() uses the default locale
String plain = new BigDecimal("1234.5").setScale(2).toPlainString(); // 1234.50, locale-independent
// User-facing: the reader's locale, from the request or profile
Locale userLocale = Locale.forLanguageTag("de-DE");
NumberFormat nf = NumberFormat.getNumberInstance(userLocale);
nf.setMinimumFractionDigits(2);
System.out.println(nf.format(total)); // 1.234,50

Notice the second line: formatted() has no locale parameter, so it always uses the default and is not a fix for machine output. For money, BigDecimal with toPlainString avoids both the locale and binary floating-point rounding. For people, formatting in their locale is the correct behaviour, so a German user should see 1.234,50, and the locale should come from the request, for example Spring's LocaleContextHolder, not from the server.

Setting -Duser.language=en on every JVM can be a useful safety net, but it hides the problem instead of fixing it. The code should state its intent so it behaves the same in tests, on servers and in other teams' environments.

5The Same Trap in Case Conversion

toUpperCase() and toLowerCase() without arguments also use the default locale, and the Turkish and Azerbaijani locales have a famous difference: they have a dotted and a dotless i. In Turkish, the uppercase of i is I with a dot above, and the lowercase of I is dotless i.

Java
Locale tr = Locale.of("tr"); // Java 19+; new Locale("tr") before that
System.out.println("TITLE".toLowerCase(tr)); // tıtle: dotless i, so not "title"
System.out.println("title".toUpperCase(tr)); // TİTLE: I with a dot above
System.out.println("TITLE".toLowerCase(tr).equals("title")); // false
System.out.println("TITLE".toLowerCase(Locale.ROOT)); // title, on every server

This breaks code that normalises keys, compares header names, maps enum names or checks file extensions on a server whose default locale is Turkish. The fix is the same: use toLowerCase(Locale.ROOT) for identifiers and protocol values, and equalsIgnoreCase for comparisons, which is not locale-sensitive.

6Follow-up questions

What does "TITLE".toLowerCase() return in a Turkish locale?

It returns t, dotless i (U+0131), t, l, e. The Turkish rules map capital I to dotless lowercase i, so the result looks like title but is not equal to it. Code such as if (header.toLowerCase().equals("title")) silently fails on such a server, which is why identifiers should be lowercased with Locale.ROOT.

Is Locale.ROOT the same as Locale.US?

For numbers they happen to give similar output, but they mean different things. Locale.ROOT is the neutral, language-independent locale intended for machine-readable output, while Locale.US is a real locale whose data could change with CLDR updates. For stable formats, Locale.ROOT states the intent better.

How do we find all the places that rely on the default locale?

Static analysis helps: the forbidden-apis tool and Error Prone have checks that flag locale-sensitive methods called without a Locale, such as String.format, toUpperCase and toLowerCase. Running tests with an unusual default locale, for example -Duser.language=tr, also shakes these bugs out quickly.

7Conclusion

In this answer, we saw that String.format without a Locale uses the JVM's default locale, which comes from the operating system or startup properties, so locales such as German print 1234,50 instead of 1234.50. We fixed it by choosing the locale on purpose: Locale.ROOT or a locale-independent API such as BigDecimal.toPlainString for machine-readable output, and the user's locale for text people read, remembering that formatted() always uses the default. The same applies to case conversion, where the Turkish locale turns TITLE into a dotless-i version of title, so identifiers should use toLowerCase(Locale.ROOT).

What to say in the interviewAbout 90 seconds aloud

Open with

String.format without a locale argument uses the JVM's default locale, and that default comes from the environment: LANG or LC_ALL on Linux, the regional settings on Windows, or user.language if someone set it.

Then, if they want more

In locales like German or French, the decimal separator is a comma, so the same code prints 1234.50 on one server and 1234,50 on another, typically a server in another region or a container image with a different language setting. Double.toString is not affected, only the locale-aware formatting APIs. The fix is to decide who reads the output.

If it is a machine, like CSV, JSON or logs a tool parses, I pass Locale.ROOT explicitly, so String.format with Locale.ROOT, and for money I would rather use BigDecimal and toPlainString. Note that formatted has no locale parameter, so it always uses the default and is not a fix there.

If a person reads it, I format with their locale, taken from the request, not the server, so a German user correctly sees 1.234,50. The same trap exists in toUpperCase and toLowerCase. In a Turkish locale, TITLE lowercased gives a dotless i, so it no longer equals title, which breaks header checks, enum lookups and so on.

So identifiers get toLowerCase with Locale.ROOT. To catch the rest, I would enable a static check for locale-sensitive calls without a locale, and run the tests once with the default locale set to Turkish.

About this answer

Last updated
12 November 2026
Versions it names
Java 19
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 "Why can String.format("%.2f", 1234.5) print "1234,50" on some servers, and how do you...": https://scrivio-interview-library.netlify.app/java/strings/3f199aaa14/