How can I format a String number to have commas an

2019-01-02 20:24发布

What is the best way to format the following number that is given to me as a String?

String number = "1000500000.574" //assume my value will always be a String

I want this to be a String with the value: 1,000,500,000.57

How can I format it as such?

11条回答
不再属于我。
2楼-- · 2019-01-02 20:32

The first answer works very well, but for ZERO / 0 it will format as .00

Hence the format #,##0.00 is working well for me. Always test different numbers such as 0 / 100 / 2334.30 and negative numbers before deploying to production system.

查看更多
梦醉为红颜
3楼-- · 2019-01-02 20:39

you can also use below solution -

public static String getRoundOffValue(double value){

        DecimalFormat df = new DecimalFormat("##,##,##,##,##,##,##0.00");
        return df.format(value);
}
查看更多
看风景的人
4楼-- · 2019-01-02 20:39

Here is the simplest way to get there:

String number = "10987655.876";
double result = Double.parseDouble(number);
System.out.println(String.format("%,.2f",result)); 

output: 10,987,655.88

查看更多
裙下三千臣
5楼-- · 2019-01-02 20:50

This class is for simple Indian price formatter

public class Test {

public static void main (String a[]){
    System.out.println(priceFormatter("10023"));    
}
/**
 * This method is to convert the price into Indian price
 * separator format
 * @param inputPrice
 * @return
 */
public static String priceFormatter(String inputPrice){
    try {
        if(!isNumeric(inputPrice)){
            return inputPrice;
        }
        // to check if the number is a decimal number
        String newPrice = "",afterDecimal = "";
        if(inputPrice.indexOf('.') != -1){
            newPrice = inputPrice.substring(0,inputPrice.lastIndexOf('.'));
            afterDecimal = inputPrice.substring(inputPrice.lastIndexOf('.'));
        }else{
            newPrice = inputPrice;
        }
        int length = newPrice.length();
        if (length < 4) {
            return inputPrice;
        }
        // to check whether the length of the number is even or odd
        boolean isEven = false;
        if (length % 2 == 0) {
            isEven = true;
        }
        // to calculate the comma index
        char ch[] = new char[length + (length / 2 - 1)];
        if (isEven) {
            for (int i = 0, j = 0; i < length; i++) {
                ch[j++] = inputPrice.charAt(i);
                if (i % 2 == 0 && i < (length - 3)) {
                    ch[j++] = ',';
                }
            }
        } else {
            for (int i = 0, j = 0; i < length; i++) {
                ch[j++] = inputPrice.charAt(i);
                if (i % 2 == 1 && i < (length - 3)) {
                    ch[j++] = ',';
                }
            }
        }
        // conditional return based on decimal number check
        return afterDecimal.length() > 0 ? String.valueOf(ch) + afterDecimal : String.valueOf(ch);

    } catch(NumberFormatException ex){
        ex.printStackTrace();
        return inputPrice;
    }
      catch (Exception e) {
        e.printStackTrace();
        return inputPrice;
    }
}

}
查看更多
爱死公子算了
6楼-- · 2019-01-02 20:51

You can do the entire conversion in one line, using the following code:

String number = "1000500000.574";
String convertedString = new DecimalFormat("#,###.##").format(Double.parseDouble(number));

The last two # signs in the DecimalFormat constructor can also be 0s. Either way works.

查看更多
牵手、夕阳
7楼-- · 2019-01-02 20:52

This can also be accomplished using String.format(), which may be easier and/or more flexible if you are formatting multiple numbers in one string.

    String number = "1000500000.574";
    Double numParsed = Double.parseDouble(number);

    System.out.println(String.format("The input number is: %,.2f", numParsed));
    // Or
    String numString = String.format("%,.2f", numParsed);

For the format string "%,.2f" - "," means separate digit groups with commas, and ".2" means round to two places after the decimal.

For reference on other formatting options, see https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

查看更多
登录 后发表回答