This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes question about decimal output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "question about decimal output" Watch "question about decimal output" New topic

question about decimal output

dean martin

Joined: Nov 17, 2003
Posts: 1
Ok, I have a java console program with a few equations, when the equation runs, the solution is a number like 31.5312256323421616
How do i get java to print out this with just the first two spots after the decimal? (31.53)
I know that in C programming you simply do %.2f but i can't figure out how to do it in java. By the way, my java variable is a double.. thanks guys.
Arun Boraiah
Ranch Hand

Joined: Nov 28, 2001
Posts: 233
You can explore java.text.DecimalFormat class to fulfill your requirement.

Sharing is learning
It is sorta covered in the JavaRanch Style Guide.
subject: question about decimal output
Similar Threads
Formatting number values code example needed
Hexadecimal Numbers
Explanation needed for this program
displaying decimal numbers
rounding off in java