How to use decimal format in Java

6 Answers

0 votes
package javaapplication1;

import java.text.DecimalFormat;

public class Example {
    public static void main(String[] args) {
			
        double d = 123456873.7891;
        DecimalFormat dFormat = new DecimalFormat("###,###.###");
	    System.out.println(d + "  " + "###,###.###" + "  " + dFormat.format(d));
    }
}
 
 
/*
run:
 
1.234568737891E8  ###,###.###  123,456,873.789
 
*/

 



answered Jan 12, 2016 by avibootz
0 votes
package javaapplication1;

import java.text.DecimalFormat;

public class Example {
    public static void main(String[] args) {
			
        double d = 123456.7899;
        DecimalFormat dFormat = new DecimalFormat("###,###.###");
	    System.out.println(d + "  " + "###,###.###" + "  " + dFormat.format(d));
    }
}
 
 
/*
run:
 
123456.7899  ###,###.###  123,456.79
 
*/

 



answered Jan 12, 2016 by avibootz
0 votes
package javaapplication1;

import java.text.DecimalFormat;

public class Example {
    public static void main(String[] args) {
			
        double d = 123456.789;
        DecimalFormat dFormat = new DecimalFormat("###.##");
	    System.out.println(d + "  " + "###.##" + "  " + dFormat.format(d));
    }
}
 
 
/*
run:
 
123456.789  ###.##  123456.79
 
*/

 



answered Jan 12, 2016 by avibootz
0 votes
package javaapplication1;

import java.text.DecimalFormat;

public class Example {
    public static void main(String[] args) {
			
        double d = 1234569.89;
        DecimalFormat dFormat = new DecimalFormat("$###,###.###");
	    System.out.println(d + "  " + "$###,###.###" + "  " + dFormat.format(d));
    }
}
 
 
/*
run:
 
1234569.89  $###,###.###  $1,234,569.89
 
*/

 



answered Jan 12, 2016 by avibootz
edited Jan 13, 2016 by avibootz
0 votes
package javaapplication1;

import java.text.DecimalFormat;

public class Example {
    public static void main(String[] args) {
			
        double d = 1269.89;
        DecimalFormat dFormat = new DecimalFormat("000000.000");
	    System.out.println(d + "  " + "000000.000" + "  " + dFormat.format(d));
    }
}
 
 
/*
run:
 
1269.89  000000.000  001269.890
 
*/

 



answered Jan 12, 2016 by avibootz
0 votes
package javaapplication1;

import java.text.DecimalFormat;

public class Example {
    public static void main(String[] args) {
			
        DecimalFormatF(123456873.7891, "###,###.###");
        DecimalFormatF(123456.789, "###.##");
        DecimalFormatF(1234569.89, "$###,###.###");
        DecimalFormatF(1269.89, "000000.000");
    }
    static public void DecimalFormatF(double val, String pattern) {
	    DecimalFormat dFormat = new DecimalFormat(pattern);
	    System.out.println(val + "  " + pattern + "  " + dFormat.format(val));
    }
}
 
 
/*
run:
 
1.234568737891E8  ###,###.###  123,456,873.789
123456.789  ###.##  123456.79
1234569.89  $###,###.###  $1,234,569.89
1269.89  000000.000  001269.890
 
*/

 



answered Jan 13, 2016 by avibootz

Related questions

1 answer 99 views
1 answer 167 views
3 answers 295 views
...