Use comma Separator to format a number - Java java.lang

Java examples for java.lang:double Format

Description

Use comma Separator to format a number

Demo Code


//package com.java2s;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class Main {
    public static void main(String[] argv) {
        String number = "31231";
        System.out.println(commaSeparator(number));
    }//  w w  w .  jav a2 s. c o  m

    public static String commaSeparator(String number) {

        Double d = Double.parseDouble(number);

        DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
        symbols.setGroupingSeparator('-');

        DecimalFormat formatter = new DecimalFormat("###,###", symbols);

        String str = formatter.format(d);

        return str;
    }
}

Related Tutorials