Question

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

Était-ce utile?

La solution 2

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

Autres conseils

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));

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top