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.

4

1 回答 1

2

不是真正的答案,但这会打印 2(Java JDK,而不是 Android):

public static void main(String[] args) {
    Locale currencyLocale = new Locale("es","CR");
    Currency currency = Currency.getInstance(currencyLocale);
    System.out.println(currency.getDefaultFractionDigits());
}
于 2012-07-16T14:10:05.063 回答