mobilemagic
mobilemagic

Reputation: 273

Conversion of string numbers into comma seperated string numbers in Android

I am currently working on an Android project where we use string numbers with many digits in it.

So I want to know whether there is a way to convert the string numbers for e.g 1000000000 into comma separated string numbers for e.g(1,00,00,00,000) in Indian Locale format.

I got the US locale format string conversion but I want Indian locale format.

Upvotes: 0

Views: 1979

Answers (4)

hemal237
hemal237

Reputation: 23

The answer given by Pankaj Kumar gives the output in US format not in Indian Format. If you want in US format it can be easily done by following code:

NumberFormat.getNumberInstance(Locale.US).format(35634646)

As shown here: Converting Integer to String with comma for thousands

To get the string with commas in Indian Format you can try this manual code

public  String round(String d)
{


    ArrayList<Integer> commas=new ArrayList<Integer>();
    String output=null;
    char[] preDecimal=d.split("[.]")[0].toCharArray();
    int i=preDecimal.length-3;
    System.out.println(i);
    while(i>0)
    {
        commas.add(i);
        i-=2;
    }

    StringBuilder sb=new StringBuilder();
    for(i=0;i<preDecimal.length;i++)
    {
        sb.append(preDecimal[i]);
        if(commas.contains(i+1))
            sb.append(",");



    }

       output=sb.toString();

    return output;

}

Upvotes: 0

Pankaj Kumar
Pankaj Kumar

Reputation: 82958

Ref : Formatting a Number Using a Custom Format

private String getFormatedData(String unformatedData) {
        if(unformatedData != null) {
            try {   
                //unformatedData.replaceAll(",", "");
                Double result = Double.valueOf(unformatedData);             
                DecimalFormat myFormatter = new DecimalFormat("###,##0.00");
                            //DecimalFormat myFormatter = new DecimalFormat("#,###,###");
                            //If you don't want to show .00 format 
                return myFormatter.format(result);
            } catch (NumberFormatException e) {
                return unformatedData;
            }

        } else {
            return "0.00";
        }       
    }

Use this method.

I think you have to modify this method if you don't need .00 value. Let me work on it.

Upvotes: 1

curioustechizen
curioustechizen

Reputation: 10672

I would use format("% ,d", number) method of Format class, ensuring that I initialize the Formatter object with the appropriate locale (which I believe is en_IN for Indian).

Having said that, it would be easier for people to help you if you posted code on how you are doing it for US locale in the first place.

Upvotes: 1

StErMi
StErMi

Reputation: 5469

I think that you need to use NumberFormat so you can make a general case using current Locale settings of the user.

And I think that this is your current situation:

If you are formatting multiple numbers, it's more efficient to get the format and use it multiple times so that the system doesn't have to fetch the information about the local language and country conventions multiple times.

 NumberFormat nf = NumberFormat.getInstance();
 for (int i = 0; i < a.length; ++i) {
     output.println(nf.format(myNumber[i]) + "; ");
 }

Upvotes: 0

Related Questions