• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method for limiting decimal places?

 
rsb
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean when you print the number out, yes? Look at java.text.DecimalFormat.
For example, this code prints "123.46":
 
rsb
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks.
-Barry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic