Reputation: 2630
I am trying to display a price for an item, in the relative currency given with the data, in the users locale format.
e.g. The item is in EUR and in UK should be displayed as €123.64 but in France should be displayed as 123,64 €
I want the currency symbol and decimal separator to be placed based on the users locale.
However, the piece that is getting me stuck is how to strip the .00 if the item has no pence/cents value.
I have tried using
NumberFormat f = NumberFormat.getInstance(loc);
if (f instanceof DecimalFormat) {
((DecimalFormat)f).setDecimalSeparatorAlwaysShown(false);
}
from DecimalFormat.html#setCurrency
but it doesn't have a currency symbol, even when I try f.setCurrency().
I have also tried changing getInstance to getCurrencyInstance, which now applies the currency symbol, but ignores the setDecimalSeparatorAlwaysShown(false) part.
I've thought about checking to see if the remainder is 0, and then stripping it out, but that would require me checking/knowing what the decimal separator is, which although possibly doable, is pretty hacky.
Does anyone know a way of doing all these things?!
Upvotes: 10
Views: 12041
Reputation: 2630
Ok, I've been completely stupid and blind, and didn't notice the method
I've now set this to 0 if there is the decimal part is 0.
Upvotes: 29
Reputation: 11
for the remove the 00, a simple hack for just euro/england would be to do a string replace for ".00" and just replace with "". But some currencies have 3 or 1 or 0 decimal digits so keep that in mind.
This is what I used to take an int value from my db and diplay it in a currency format and stay like that while users entered a currency value, also converts the currency string back into an int, may be of help https://github.com/nleigh/Restaurant/blob/master/Restaurant/src/uk/co/nathanleigh/restaurant/CurrencyFormat.java
Upvotes: 0