Luis Aguilar
Luis Aguilar

Reputation: 735

How to add comma every 3 digits without using number format?

I am trying to format a value,

Example:

1526374856.03

to:

1,526,374,856.03

Upvotes: 4

Views: 7696

Answers (5)

user837086
user837086

Reputation: 1

I use next code, works quickly

public static String addCommaEvery3Digits(String number) {
    if (number == null) {
        return number;
    }

    int startPos = 0;
    for (int i = 0, size = number.length(); i < size; i++) {
        if (Character.isDigit(number.charAt(i))) {
            break;
        } else {
            startPos++;
        }
    }

    for (int i = startPos, size = number.length(); i < size; i++) {
        boolean d = Character.isDigit(number.charAt(i));
        if (!d || i + 1 == size) {
            if (i - startPos > 2) {
                StringBuilder sb = new StringBuilder(number);
                for (int j = i - (d ? 2 : 3); j > startPos; j -= 3) {
                    sb.insert(j, ',');
                }
                return sb.toString();
            }
            break;
        }
    }
    return number;
}

Upvotes: 0

Mohammad Moradi
Mohammad Moradi

Reputation: 1

I came up with this

    private String formatNumber(String number) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(number);
        String string = String.valueOf(number);

        if(string.contains("."))
            string= (String) string.subSequence(0,string.indexOf("."));

        if(string.length() > 3) {
            int firstComma=(string.length() % 3);
            int countComma = (string.length()-1)/3;

            if(firstComma != 0)
                stringBuilder.insert(firstComma, ",");

            for(int i = stringBuilder.indexOf(",")+4; i<string.length()+countComma; i+=4)
                stringBuilder.insert(i, ",");
        }
        return stringBuilder.toString();
    }

Upvotes: 0

Luis Aguilar
Luis Aguilar

Reputation: 735

This is the correct way to set the amount formats in android. Initialize NumberFormat variable as shown below and the just call the Numberformat variable name and the method format.

In This case my boolean number is any_number variable and I specify Locale.US because of the country.

 NumberFormat nf = NumberFormat.getInstance(Locale.US);

    nf.format(any_number);

Hope this helps some one.

Upvotes: 3

KeLiuyue
KeLiuyue

Reputation: 8237

Try this

/**
 * data format
 *
 * @param data
 * @return
 */
public static String bigDecimalData(String data) {
    if (!TextUtils.isEmpty(data)) {
        BigDecimal bd = new BigDecimal(Double.parseDouble(data));
        DecimalFormat df = new DecimalFormat(",###,####.00");
        return df.format(bd);
    }
    return "";
}

Upvotes: 1

Ishaan Javali
Ishaan Javali

Reputation: 1713

To do it without using NumberFormat, you can convert the number to a String and do the following code:

double number = 1526374856.03;
String[] array = Double.toString(number).split(".");
String numString = array[0];

String newString = "";
for(int i = 0; i < numString.length() ; i++){
    if((numString.length() - i - 1) % 3 == 0){
        newString += Character.toString(numString.charAt(i)) + ",";
    }else{
        newString += Character.toString(numString.charAt(i));
    }
}
newString += array[1];

newString is now the new String that contains the number with the commas.

Upvotes: 0

Related Questions