Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way to format numbers in a locale aware way?

Tags:

java

android

Lets assume we have one million.

In English it should be formatted as 1,000,000 in German it should be 1.000.000.

like image 962
mibollma Avatar asked Dec 07 '10 10:12

mibollma


People also ask

What is locale in string format?

locale which is the locale value to be applied on the this method. format which is the format according to which the String is to be formatted. args which is the number of arguments for the formatted string. It can be optional, i.e. no arguments or any number of arguments according to the format.

How do you use number format classes?

The NumberFormat class is used to format the number according to the specific locale. To get the instance of the NumberFormat class, we need to call either getInstance() or getNumberInstance() methods. Syntax of these methods is given below: public static NumberFormat getNumberInstance(Locale locale)


2 Answers

Using NumberFormat class:

For English:

NumberFormat nf_us = NumberFormat.getInstance(Locale.US);
String number_us = nf_us.format(1000000);

For German:

NumberFormat nf_ge = NumberFormat.getInstance(Locale.GERMAN);
String number_ge = nf_ge.format(1000000);
like image 50
Jesus Oliva Avatar answered Sep 22 '22 18:09

Jesus Oliva


You can use NumberFormat.

Android documentation is quite clear on it.

like image 44
Riduidel Avatar answered Sep 19 '22 18:09

Riduidel