Nov
15
|
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);
Array ( ) One Response to “Creating BigDecimal from localized strings”
Leave a Reply
You must be logged in to post a comment.
September 12th, 2012 at 8:57 pm
Thanks a lot, that really helped me!