Page 1 of 1

How to I disply only 3 numbers after dot of double?

Posted: Sat Nov 22, 2008 12:12 pm
by barakmitz
Hi,
I want to sidply Double variables but I want it always to be 3 numbers after the dot
Example:
4 ---> 4.000
1642.63787565 ----> 1642.638

and so on
how can i do this?
I heard something about printf but im not sure how to use it or if it works...

Thanks

Re: How to I disply only 3 numbers after dot of double?

Posted: Sat Nov 22, 2008 1:04 pm
by mf
System.out.printf("%.3f\n", Math.PI);

Re: How to I disply only 3 numbers after dot of double?

Posted: Sun Dec 28, 2008 9:08 pm
by cjrr2046
double a1=2.02356323; // Any double //
DecimalFormat df=new DecimalFormat ("0.000");
String a2=df.format (a1);
System.out.println (a2);

DecimalFormat is in this package: java.text.DecimalFormat;

To format a double...

Posted: Sat May 02, 2009 6:42 am
by jpaul
Hi!
You can use DecimalFormat for create someone format for the floating point numbers, as double, but you don't need that... You should use the next form for print data, I test that is the faster form for print data in Java.

Code: Select all

PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// use for all output

out.printf("%.3f\n", Math.PI);

// use to endish
out.flush();