Reputation: 54811
I'm familiar with:
import java.text.NumberFormat;
import java.util.Locale;
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
System.out.println(fmt.format(120.00));
as a method, only I have a java.util.Currency
, not a locale. There seems to be no way to get the locale from the Currency
only the other way around.
I've done this for now:
public String format(Currency currency, long value) {
int defaultFractionDigits = currency.getDefaultFractionDigits();
NumberFormat format = NumberFormat.getInstance();
format.setMinimumFractionDigits(defaultFractionDigits);
format.setMaximumFractionDigits(defaultFractionDigits);
double divider = Math.pow(10, defaultFractionDigits);
return String.format("%s %s", currency.getSymbol(Locale.getDefault()), format.format(value/divider));
}
So I'm wondering if there's a clearner way?
Note value
is defined in the smallest part of the currency, e.g. a cent or a penny. But that's not important for purpose of question, would take answer that formatted a floating value.
Upvotes: 0
Views: 178