Results 1 to 6 of 6
  1. #1
    darek9576 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default Limiting decimal places in a double

    When i write in my program

    double pi = Math.PI;

    This will give me a value of PI accurate to many decimal places, but what if i want to limit this number
    lets say to 3 decimal places..
    Is there such a method in Java?

    (Im not talking here only about the value of Math.PI or Math.E, but about all double values that contain
    many decimal places)

  2. #2
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    @WillRobinson

    Danger! Danger! Link to RoseIndia!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    "Decimal places" - indeed even the concept of "decimal" - refers to the string form that numbers often have. It's not quite true, but almost always, when you are talking about "decimal places" you are talking about strings and formatting.

    Use the DecimalFormat class. Or java.util.Formatter. The latter class is used with the likes of the String format() method and is very flexible.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Agree. Avoid RoseIndia unless you want advice that may or may not be correct.

    The question I would like to ask is this: do you truly want to round the number or the display of numbers? Often what matters is the latter, and if that is your case, then you may wish to look into the String.format/Formatter/printf family or DecimalFormat.

    Edit: too late! I saw pbrock's first post but not the second. OP, please look at his links for they show the "True Way"™
    Last edited by Fubarable; 03-14-2010 at 02:07 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I always forget, but Oracle's Tutorial was - long ago in sunnier days - updated to have a "by example" page on Formatting Numeric Print Output. The examples I linked to are good reference material but the tutorial might provide something that is a little less heavy going.

Similar Threads

  1. round to two decimal places
    By javaMike in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 03:01 AM
  2. rounding double to two decimal places
    By javaMike in forum Advanced Java
    Replies: 15
    Last Post: 03-10-2010, 01:04 AM
  3. [SOLVED] Decimal Places - Is this possible?
    By dbashby in forum New To Java
    Replies: 4
    Last Post: 04-08-2009, 08:49 PM
  4. Replies: 4
    Last Post: 03-03-2009, 01:43 PM
  5. Capping decimal places
    By Rageagainst20 in forum New To Java
    Replies: 1
    Last Post: 12-20-2007, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •