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
How to I disply only 3 numbers after dot of double?
Moderator: Board moderators
Re: How to I disply only 3 numbers after dot of double?
System.out.printf("%.3f\n", Math.PI);
Re: How to I disply only 3 numbers after dot of double?
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;
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...
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.
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();