Java - NumberValue to String without Currency sign
Asked Answered
R

2

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

Ratcliff answered 30/1, 2019 at 11:28 Comment(10)
How does 1.1 -> 1,01 make sense? What's the logic?Cinematography
@ernest_k, sorry, that was a typo. I'll correct it ( the one below as well ).Ratcliff
NumberFormat instance = NumberFormat.getInstance(); instance.setMinimumFractionDigits(2);Propst
@Propst 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: apps.cybersource.com/library/documentation/sbc/quickref/…Ratcliff
@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.Ratcliff
ohh, now I got it,Dishman
See this question: #8658705Isom
@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.Propst
@BorLaze, thank you for the link! I just tried it and it does seem to work! I'll close the question.Ratcliff
Try using DecimalFormat, see this question, the output will use the decimal character for the current localeSlobbery
D
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.

Demonology answered 30/1, 2019 at 11:50 Comment(2)
I actually like this solution much better then the linked one, it looks less like a workaround. Thank you very much for your help!Ratcliff
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)." - learn.microsoft.com/en-us/globalization/locale/…Flaring
C
-1

Here is a more robust approach (for some currencies minimumFractionDigits and maximumFractionDigits are not necessarily equal to Currency.getDefaultFractionDigits()):

val numberFormat = NumberFormat.getNumberInstance().apply {
    val priceFormat = NumberFormat.getCurrencyInstance()
    minimumFractionDigits = priceFormat.minimumFractionDigits
    maximumFractionDigits = priceFormat.maximumFractionDigits
}
Crippen answered 10/11, 2023 at 8:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.