2

I am trying to format a NumberValue as a String in the current locale format.

For example, for Locale.Germany these would be the results I am trying to get:

1   -> 1,00
1.1 -> 1,10
0.1 -> 0,10

What I have tried:

String test1 = NumberFormat.getInstance().format(1.1);
// -> 1,1
String test2 = NumberFormat.getCurrencyInstance().format(1.1);
// -> 1,10 €

How could I get the same format as the second example, without the currency symbol? Or alternativly, how could I make sure the result from the first example always has the amount of decimals needed for the current locale?

Edit: Since there are some currencies that have 0,2 or 3 decimals, I don't think setting the decimals to a fixed amount will have the correct results.

Also, some currencies have their symbol in the front, and some in the back, so cutting off the last characters will probably not work either.

http://apps.cybersource.com/library/documentation/sbc/quickref/currencies.pdf

Rence
  • 2,900
  • 2
  • 23
  • 40
  • How does `1.1 -> 1,01` make sense? What's the logic? – ernest_k Jan 30 '19 at 11:29
  • @ernest_k, sorry, that was a typo. I'll correct it ( the one below as well ). – Rence Jan 30 '19 at 11:30
  • `NumberFormat instance = NumberFormat.getInstance(); instance.setMinimumFractionDigits(2);` – davidxxx Jan 30 '19 at 11:35
  • @davidxxx will this work for all Locales / Currencies? I had assumed there are some that might use more decimals or less. Edit: I found a list, there are some listed that have 0 or 3: http://apps.cybersource.com/library/documentation/sbc/quickref/currencies.pdf – Rence Jan 30 '19 at 11:36
  • @dkb, thank you for the link, but I seem to be missing something - the example seems to use the same function as my second example, with the same result. – Rence Jan 30 '19 at 11:39
  • ohh, now I got it, – dkb Jan 30 '19 at 11:44
  • See this question: https://stackoverflow.com/questions/8658205/format-currency-without-currency-symbol – Bor Laze Jan 30 '19 at 11:45
  • @Sirence Of course it will not. The currency format is implemented in the currency style format. You should customize the instance returned by `NumberFormat.getCurrencyInstance()` to remove the symbol part. – davidxxx Jan 30 '19 at 11:48
  • @BorLaze, thank you for the link! I just tried it and it does seem to work! I'll close the question. – Rence Jan 30 '19 at 11:49
  • Try using `DecimalFormat`, see [this question](https://stackoverflow.com/questions/54402352/is-it-possible-to-change-the-decimal-format-of-the-entire-code-which-includes-sw/54403485#54403485), the output will use the decimal character for the current locale – Joakim Danielson Jan 30 '19 at 11:50

1 Answers1

5

java.util.Currency provides the necessary information. A reasonable approach to print currency values without the currency sign in pure Java code is to get a general-purpose NumberFormat instance and configure it to use the number of digits defined by the appropriate Currency object:

NumberFormat numberFormat = NumberFormat.getInstance(myLocale);
Currency currency = Currency.getInstance(myLocale);
numberFormat.setMinimumFractionDigits(currency.getDefaultFractionDigits());

If you need a specific currency as opposed to the default currency of the locale you use, use the String override of Currency.getInstance() instead and provide the appropriate currency code.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • I actually like this solution much better then the linked one, it looks less like a workaround. Thank you very much for your help! – Rence Jan 30 '19 at 11:55
  • 2
    This is my favourite answer so far, but it isn't flawless. "Most currencies use the same decimal and thousands separator that other numbers in the locale use, but this is not always true. In some places in Switzerland, they use the period as a decimal separator for Swiss francs (Sfr. 127.54), but then use commas as the decimal separator everywhere else (127,54)." - https://learn.microsoft.com/en-us/globalization/locale/currency-formatting – AndroidKotlinNoob Jul 27 '22 at 11:54