iberck
2/15/2016 - 11:04 PM

java-bigdecimal-localization.md

java-bigdecimal-localization.md

The BigDecimal constructors do not take the Locale into account when parsing number strings. This means the following code will throw a NumberFormatException:

Locale.setDefault(new Locale("nl", "NL"));
String s =  "2.343.298,09324798";
BigDecimal bd = new BigDecimal(s);

To parse localized strings as BigDecimal we instead need to use the DecimalFormat class:

Locale.setDefault(new Locale("nl", "NL"));
String s =  "2.343.298,09324798";
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
df.setParseBigDecimal(true);
BigDecimal bd = (BigDecimal) df.parse(s);