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: , , , ,

Apr 29

Sometimes you need to get the currency symbol used for your currency in a different Locale. eg. In the US they use $, but a different Locale may use US$.

The Currency class can be used to not only get the currency symbol for a Locale, but also the currency symbol used for your currency in a different Locale.

Currency currency = Currency.getInstance(myLocale);
String symbol = currency.getSymbol(otherLocale);

written by objects \\ tags: , ,

Apr 25

The Currency class can be used to get the currency symbol used for a particular Locale as shown in the following example.

Currency currency = Currency.getInstance(locale);
String symbol = currency.getSymbol();

written by objects \\ tags: , ,