Tuesday, 19 April 2011

Currency Symbols

Had an issue recently where some unit tests that were working on Windows didn't work on OSX. This snippet sums up why:
public void testCurrencySymbol() {
Locale locale = new Locale("en", "GB");
String formattedPrice = Currency.getInstance("EUR").getSymbol(locale) + "10.00";
String badFormattedPrice = Currency.getInstance("EUR").getSymbol() + "10.00";
assertEquals(formattedPrice, badFormattedPrice); // fails on OSX!
}

Seems a bit weird, but it's sort of logical - you have to pass in your target locale when you get the currency symbol for some other locale.

No comments: