sarath kumar
sarath kumar

Reputation: 219

How to format number with special character using DecimalFormat in java

I would like to format numbers like 12-345-67 in java. Is there any possible ways to do? I have tried the below code:

double d =123456789;
DecimalFormat df = new DecimalFormat("[##'-'##'-'##]");
String result = df.format(d);//output: [123456789--]

Thanks in advance

Upvotes: 3

Views: 1536

Answers (1)

Abhishek Honey
Abhishek Honey

Reputation: 645

Here is the code you can try this. Here is the link

http://tutorials.jenkov.com/java-internationalization/decimalformat.html

The output is :: 1-234-567-890

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

class test {
    public static void main(String[] args) {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        //symbols.setDecimalSeparator(';');
        symbols.setGroupingSeparator('-');
        String pattern = "#,##0.###";
        DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);

        String number = decimalFormat.format(1234567890);
        System.out.println(number);
    }
}

Upvotes: 2

Related Questions