Wednesday, September 4, 2013

Java - Format Number

public class DecimalFormatExample {  
 
    public static void main(String args[])  {
     
        //formatting numbers upto 2 decimal places in Java
        DecimalFormat df = new DecimalFormat("#,###,##0.00");
        System.out.println(df.format(364565.14));
        System.out.println(df.format(364565.1454));
     
        //formatting numbers upto 3 decimal places in Java
        df = new DecimalFormat("#,###,##0.000");
        System.out.println(df.format(364565.14));
        System.out.println(df.format(364565.1454));
    }
     
}

Output:
364,565.14
364,565.15
364,565.140
364,565.145

0 comments:

Post a Comment