Tag Archives: math

Formatting Decimal Values in Java

I wanted to format my large double values in comma separated format. I took help from here.

 

import java.text.DecimalFormat;

 

public class DecimalFormatTest

{

private static void format(Double value, String pattern)

{

DecimalFormat decimalFormat = new DecimalFormat(pattern);

String output = decimalFormat.format(value);

System.out.println(value + ”  ” + pattern + ”  ” + output);

}

public static void main(String[] args)

{

format(new Double(“12345.67”), “###,##0.00”);

format(new Double(“12345”), “###,##0.00”);

format(new Double(“0”), “###,##0.00”);

format(new Double(“0”), “###,###.##”);

}

}