File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Method for limiting decimal places? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method for limiting decimal places?" Watch "Method for limiting decimal places?" New topic

Method for limiting decimal places?


Joined: Aug 09, 2003
Posts: 2
I'm using the double type within an equation and printing out the results. However, depending on the double types used within the equation, I can end up with several places to the right of the decimal point. Is there a method that will limit the number places to the right of a decimal point?
Thanks for your time,
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

You mean when you print the number out, yes? Look at java.text.DecimalFormat.
For example, this code prints "123.46":

[Jess in Action][AskingGoodQuestions]

Joined: Aug 09, 2003
Posts: 2
Thanks! That is exactly what I was looking for. I know it seemed obvious, but that's why I'm in the beginner area. :-)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Welcome to Java Ranch "Rsb".
Would you please take a few moments to review JavaRanch's Naming Policy and change your displayed name accordingly. Simply put: use a non-fictious first-name, last-name pair or an initial letter with a last-name.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: Method for limiting decimal places?
It's not a secret anymore!