This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes rounding off Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "rounding off" Watch "rounding off" New topic
Author

rounding off

Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
double d=3.95;
System.out.printf("%.1f",d);
output is 4........
so 3.95 gets rounded to 4.....3.91 to 3.92 is rounded to 3.9 and 3.95 to 3.99 is rounded to 4.0



output is 124 4.0

is there any other rule that i should know about or this is it......
Aneesh Vijendran
Ranch Hand

Joined: Jun 29, 2008
Posts: 125
Not sure why you want to use the

%.0f %.1f
option.

If you just print:

System.out.printf(d1 + " " + d2);

Will show you the desired effect.

Else Use java.text.DecimalFormat




Cheers
Aneesh


Cheers
Aneesh
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: rounding off
 
Similar Threads
Dan's Mock Question - Double Wrapper
question about ==
can't understand this..!!
Issue adding double value
Double Boolean Operations