Reputation: 13
I have an application that uses thousands separator (,) and decimal separator (.), I used this app on 2 tablets with the same languages (Español) on their configuration and when I do some process with numbers like 15,000.00 on the first one the answer was correct but in the second tablet the number changes to 15,00. I changed the language of the second to English, and it works, but how can i set this number format on code?
Sorry about the errors this is not my native language.
Thanks for the help
Upvotes: 1
Views: 6748
Reputation: 51411
You could format a double value in code like this:
/**
* format a number properly
* @param number
* @return
*/
public String formatDecimal(double number) {
DecimalFormat nf = new DecimalFormat("###.###.###.##0,00");
// or this way: nf = new DecimalFormat("###,###,###,##0.00");
String formatted = nf.format(number);
return formatted;
}
And then set it to a TextView:
mytextview.setText("MyDouble: " + formatDecimal(somedouble));
Upvotes: 4
Reputation: 159
You could format using
NumberFormat nf = NumberFormat.getInstance(new Locale("es", "MX")); //for example
But beware because depending on the locale, even if is the same language but different country the decimal char and grouping char my change, for example
NumberFormat nf = NumberFormat.getInstance(new Locale("es", "CO")); //displays 15.000,00
Upvotes: 2
Reputation: 3337
You can get an instance for a specific locale as specified by the android docs:
NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
However, you should not change the locale when displaying stuff to the user imho.
Upvotes: 0