Exceptional
Exceptional

Reputation: 3004

Grouping the numbers with comma in android

Eg:- double ab=1234567.00;

The expected output should be, ab=12,34,567;

But the following format gives the default three digit grouping.

DecimalFormat df_separator = new DecimalFormat("###,###,##0.00");

Also tried with,

DecimalFormat df_separator = new DecimalFormat("###,##,##0.00");

still in vain.......

Upvotes: 1

Views: 240

Answers (2)

Triode
Triode

Reputation: 11359

double ab=1234567.00;
String str = new DecimalFormat("#,##,##,###.00").format(ab);
Log.d("TAG", str);

try this.

Upvotes: 1

Manitoba
Manitoba

Reputation: 8702

Here you are sir,

NumberFormat numberFormat = NumberFormat.getInstance();
String formattedNr = numberFormat.format(12345678L);

This will give you: 12,345,678.00

Edit:

public String formatDouble(double number)
{
    String result = "";
    String numberStr = String.valueOf(number);  
    char[] charArray = numberStr.toCharArray();
    Character[] charObjectArray = ArrayUtils.toObject(charArray);
    for (int i=charObjectArray.length()-1; i>=0 i++)
    {
        if (charObjectArray[i] == ".")
        {
            result = "." + result;
            continue;
        }
        result = charObjectArray[i] + result;
        if (i % 2 == 0) result = "," + result;
    }
    return result;
}

This is pseudo code as I don't have a JVM atm but it should (almost) do the job.

Edit: Finally

Add the following jar to your project: http://icu-project.org/apiref/icu4j/com/ibm/icu/text/NumberFormat.html

Format format = com.ibm.icu.text.NumberFormat.getCurrencyInstance(new Locale("en", "in"));
System.out.println(format.format(new BigDecimal("100000000")));

Upvotes: 2

Related Questions