4

I am trying to parse and store currency amounts as BigDecimal values. I am given the locale of the currency in question, and in most cases it works fine, but I'm getting unexpected results when the currency is the Costa Rican colón.

I am informed by my Costa Rican customers that a typical currency amount might look like 1.508.534,16 with the ,16 being the fractional part (two decimal places). However, when I call Currency.getDefaultFractionDigits() it returns 0 instead of 2 as the number of fractional digits. As a result, the values I'm calculating are being wrongly truncated.

Code looks like this:

// currencyLocale is "es_CR"
Currency currency = Currency.getInstance(currencyLocale);
int scale = currency.getDefaultFractionDigits();
// scale is 0 instead of 2

BigDecimal v = new BigDecimal("12.34")
                          .setScale(scale, BigDecimal.ROUND_HALF_DOWN);
// gives 12 instead of 12.34

Note that although the end-user-visible amounts are correctly formatted for the locale with the , as the decimal separator, the data source here is providing the values as standard 1234.56 decimal amounts.

What am I doing wrong?


UPDATE

After doing some more research, and reporting the issue to Google, I am now convinced this is an Android bug. Google's response is that this works as intended, since the affected currencies can only be spent in whole-number multiples (smallest coin is 5 colón).

Google's response ignores the fact that you can quite legitimately have fractional amounts in your bank account, though, as a result of wire transfers, interest calculations, etc.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179

1 Answers1

2

Not really an answer, but this prints 2 (Java JDK, not Android):

public static void main(String[] args) {
    Locale currencyLocale = new Locale("es","CR");
    Currency currency = Currency.getInstance(currencyLocale);
    System.out.println(currency.getDefaultFractionDigits());
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 1
    That also prints 2 for me under the Java JDK, but the same code in Android returns 0. I don't know if this is an Android bug or if that particular currency is not expected to be fully supported on European devices, but in any event I've reported the bug to Google: http://code.google.com/p/android/issues/detail?id=35057&thanks=35057&ts=1342478572 – Graham Borland Jul 16 '12 at 22:51
  • 1
    Google have acknowledged that their behaviour is different from the JDK, but don't seem inclined to do anything about it. Oh well. – Graham Borland Jul 18 '12 at 12:31