Scrivio Interview library
DebuggingFoundations7 min read · updated 12 November 2026

A service parses prices from a CSV with Double.parseDouble(field). It works in tests but fails in production with NumberFormatException for values like "1,234.50" and gives wrong results for "12,5" from a German supplier after someone switched to NumberFormat.getInstance().parse. Explain and fix.

Double.parseDouble accepts only Java's own number format, with a dot as the decimal separator and no grouping commas, whatever the locale, so "1,234.50" throws. NumberFormat.getInstance() uses the JVM's default locale, which differs between machines, and its parse() quietly stops at the first character it does not expect, so "12,5" becomes 125 on an English-locale server. The fix is to parse money into BigDecimal with an explicit locale per supplier, check that the whole field was consumed, and reject values that do not match the expected format.

Why this question is asked

Prices and quantities often arrive as text written by other systems and other countries. A parser that works on our laptop can fail or, worse, return a believable wrong number on a production host with a different locale. Interviewers like this question because it combines three separate traps: the strict format of parseDouble, the hidden dependency on the default locale, and the silent partial parsing of NumberFormat, plus the question of which type should hold money at all. In this answer, we will recap what a locale changes about numbers, reproduce both failures, see why each happens, and then build a parser that is explicit about format, exact about value and strict about input.

1A Quick Recap of Locales and Number Formats

A locale is a setting that describes a language and region, such as en-US or de-DE. Among many other things, it decides how numbers are written. In the United States one thousand two hundred thirty-four and a half is written 1,234.5, with a comma to group thousands and a dot before the decimals. In Germany the same number is 1.234,5: the roles of the two characters are swapped.

Every JVM has a default locale, taken from the operating system or from the user.language and user.country settings when the JVM starts. A developer laptop, a CI runner and a container image can each have a different one, which is why code that depends on it behaves differently in each place.

The same text read three ways
TextDouble.parseDoubleNumberFormat (en-US)NumberFormat (de-DE)
"1234.50"1234.51234.5123450 (dot is grouping)
"1,234.50"NumberFormatException1234.51.234 (stops at the dot)
"12,5"NumberFormatException125 (comma is grouping)12.5
"12.5abc"NumberFormatException12.5 (rest ignored)125 (rest ignored)
Only the strict parser fails loudly; NumberFormat returns a number every time, sometimes the wrong one.

2Failure 1: parseDouble Rejects Grouped Numbers

Double.parseDouble reads the same format as a double literal in Java source code. It allows digits, one dot, an optional sign, an exponent such as e3, surrounding whitespace and even a d or f suffix. It never looks at the locale, so it never accepts a comma.

Java
Double.parseDouble("1234.50"); // 1234.5
Double.parseDouble("1,234.50"); // NumberFormatException: For input string: "1,234.50"
Double.parseDouble("12,5"); // NumberFormatException
Double.parseDouble(" 12.5 "); // 12.5: whitespace is trimmed

So the test data, typed by a developer, used plain values like 1234.50 and passed. The production file came from a spreadsheet export that added thousands separators, and the first grouped value failed. This failure is actually the friendly one: it is loud and points at the exact input.

3Failure 2: NumberFormat Uses the Wrong Locale and Stops Quietly

To accept grouped numbers, someone switched to NumberFormat.getInstance().parse(field). That method is locale-sensitive, and without an argument it uses the default locale of whatever JVM it runs on. On a server with an English default, the German value 12,5 is read with the comma as a grouping separator.

Java
NumberFormat us = NumberFormat.getInstance(Locale.US);
us.parse("12,5"); // 125: the comma is read as a thousands separator
us.parse("12.5abc"); // 12.5: parsing stops at 'a', no error
NumberFormat de = NumberFormat.getInstance(Locale.GERMANY);
de.parse("12,5"); // 12.5
de.parse("1,234.50"); // 1.234: stops at the dot, no error

Notice two separate problems. First, the locale was implicit, so the result depended on the host. Second, parse(String) only throws ParseException when it cannot read anything at the start of the string. As soon as it meets a character it does not expect, it stops and returns what it has read so far. So 1,234.50 under a German locale becomes 1.234, a thousand times too small, with no error anywhere.

There is a third, smaller problem: parse returns a Number that is usually a Long or a Double, and a double cannot hold most decimal amounts exactly. 0.1 as a double is really 0.1000000000000000055511151231257827021181583404541015625. For prices, that error leaks into totals and rounding.

4The Fix: Explicit Locale, BigDecimal and a Full-Match Check

The fixed parser makes every assumption explicit. Each supplier has a known format, so we choose the locale from the supplier's configuration, not from the JVM. We ask DecimalFormat for a BigDecimal so the amount stays exact. And we use the parse method that takes a ParsePosition, which tells us how far it got, so we can insist that the whole field was consumed.

Java
static BigDecimal parsePrice(String text, Locale supplierLocale) {
DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(supplierLocale);
format.setParseBigDecimal(true);
String s = text.strip();
ParsePosition pos = new ParsePosition(0);
BigDecimal value = (BigDecimal) format.parse(s, pos);
if (value == null || pos.getIndex() != s.length()) {
throw new IllegalArgumentException("Not a valid price: '" + text + "'");
}
return value;
}
parsePrice("1.234,50", Locale.GERMANY); // 1234.50
parsePrice("12,5", Locale.GERMANY); // 12.5
parsePrice("12,5x", Locale.GERMANY); // IllegalArgumentException: index stopped at 4 of 5
parsePrice("1,234.50", Locale.US); // 1234.50
Step throughParsing one price field
  1. Look up the supplier's localefrom supplier config, never the JVM default
  2. Strip whitespaceCSV cells often carry spaces
  3. Parse with DecimalFormatsetParseBigDecimal(true)
  4. Whole field consumed?
    YesKeep the BigDecimalNoReject the row with the raw text
  5. Within the expected range and scale?
    YesStore the priceNoReject and report to the supplier

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

Every step either produces an exact value or a clear error; nothing is guessed.

Notice what changed. The locale is a parameter, so the same code gives the same answer on every host. setParseBigDecimal(true) makes parse return a BigDecimal, which keeps 1234.50 exactly, including its two decimal places. And the ParsePosition check turns silent partial parsing into an error: if the index stopped before the end, something in the text was not part of the number.

NumberFormat instances are not thread-safe, so we create one per call or keep one per thread; creating them is cheap compared with reading a file. Also be aware that some locales use unusual separators, for example French uses a narrow no-break space for grouping in recent JDK locale data, so check your version and test with real supplier files. If a supplier sends a plain machine format such as 1234.50, the simplest exact parser is new BigDecimal(text), which is strict like parseDouble but keeps the value exact.

5Validate at the Boundary Instead of Guessing

It is tempting to write a clever parser that guesses whether a comma is a decimal or a grouping separator. Guessing fails on exactly the values that matter: 1,234 could be one thousand two hundred thirty-four or one point two three four. So we reject ambiguous values and ask the supplier to fix the file, or agree a single format with each supplier and record it in their configuration.

Validation belongs at the boundary, where the CSV row is read, before the value travels through the system. A rejected row with the original text and line number in the error report is easy to fix. A price that is wrong by a factor of a thousand and discovered in an invoice a month later is not.

6Follow-up questions

Why not just set the default locale of the JVM to fix it?

Because the default is global. Setting -Duser.country or calling Locale.setDefault changes date formatting, string case conversion and messages for every library in the process, and it still assumes all suppliers use the same format. An explicit locale at the call site affects only the code that needs it.

Why BigDecimal rather than double for prices?

A double stores a binary fraction, and most decimal amounts such as 0.10 have no exact binary form, so sums and rounding drift by tiny amounts that turn into wrong cents. BigDecimal stores a decimal digit string and a scale, so 1234.50 is exactly 1234.50 and arithmetic with an explicit RoundingMode matches what accountants expect.

How do you test this?

With a table of real examples from each supplier, including grouped values, negative values, empty cells, trailing text and the ambiguous 1,234 case, and with the tests run under two different default locales, for example by setting Locale.setDefault in a test fixture, to prove the result does not depend on the host.

7Conclusion

In this answer, we saw that Double.parseDouble only accepts Java's own format, so grouped values fail, and that NumberFormat.getInstance() depends on the JVM's default locale and silently stops at the first unexpected character, which turned 12,5 into 125. The fix is to parse each supplier's data with that supplier's explicit locale, into BigDecimal with setParseBigDecimal(true), check with a ParsePosition that the whole field was consumed, and reject ambiguous or malformed values at the boundary.

What to say in the interviewAbout 80 seconds aloud

Open with

There are really two bugs. First, Double.parseDouble only accepts Java's literal format: digits, a dot for decimals, no grouping separators, and it ignores the locale completely, so 1,234.50 throws NumberFormatException.

Then, if they want more

Second, when someone switched to NumberFormat.getInstance().parse, that picked up the default locale of the JVM, which is different on each host. On an English-locale server, the comma in the German value 12,5 is read as a thousands separator, so we get 125.

And parse with just a string does not fail on trailing junk; it stops at the first character it does not expect and returns the partial number, so a German parser reading 1,234.50 silently returns 1.234.

To fix it, I would make each supplier's locale explicit in configuration and parse with NumberFormat.getInstance of that locale, cast to DecimalFormat with setParseBigDecimal true, because prices should be BigDecimal, not double. I would use the parse overload with a ParsePosition and reject the value unless the index reached the end of the string.

And I would validate at the boundary: if a value is ambiguous, like 1,234, I reject the row and report it rather than guess. I would test with real supplier samples under two different default locales to prove the host no longer matters.

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 service parses prices from a CSV with Double.parseDouble(field). It works in tests but...": https://scrivio-interview-library.netlify.app/java/types-and-values/8b910050a2/