1

I have this function:

public void printCurrency(double currencyAmount, String lang, String country) {
    Locale locale = new Locale(lang, country);
    Currency currency = Currency.getInstance(locale);
    NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
    numberFormat.setMaximumFractionDigits(2);

    Log.d(TAG, currency.getDisplayName() + ": " + numberFormat.format(currencyAmount));
}

Everytime I call this with the following:

double amount = 123456789012345.67d;
printCurrency(amount, "jp", "JP");
printCurrency(amount, "en", "GB");
printCurrency(amount, "en", "US");

My amount is always rounded like so:

> Japanese Yen: ¥123,456,789,012,346
> British Pound Sterling: £123,456,789,012,346.00 
> US Dollar: $123,456,789,012,346.00

I need a solution that always give a 2-decimal-place amount (if applicable) such as

> Japanese Yen: ¥123,456,789,012,346
> British Pound Sterling: £123,456,789,012,345.67
> US Dollar: $123,456,789,012,345.67

I tried the following but same issue:

NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
numberFormat.setMaximumFractionDigits(2);
numberFormat.setRoundingMode(RoundingMode.UNNECESSARY);
user1506104
  • 6,554
  • 4
  • 71
  • 89

1 Answers1

0

I want to post my test result here because I don't think there is something wrong with my code.

Given the following values:

amount = 1234567890123.67;
amount = 12345678901234.67;
amount = 123456789012345.67;

my function prints the following amounts:

British Pound Sterling: £1,234,567,890,123.67
British Pound Sterling: £12,345,678,901,234.70
British Pound Sterling: £123,456,789,012,346.00

As you can see, there is no rounding of amount until length is more than 15 digits.

EDIT: Tried the amount = 12345678901234567890d, result is

British Pound Sterling: £12,345,678,901,234,600,000.00

So I guess, this approach is limited to amount length <= 15.

user1506104
  • 6,554
  • 4
  • 71
  • 89