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);

written by objects \\ tags: , , , ,


One Response to “Creating BigDecimal from localized strings”

  1. cavani Says:

    Thanks a lot, that really helped me!

Leave a Reply

You must be logged in to post a comment.