Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Limiting decimal places in a double

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Limiting decimal places in a double

    yes... to show the output into 3 decimal place.. you have to use..

    System.out.printf ("%.3f" , Math.PI);

    This will convert the output into 3 decimal places. In other output system like System.out.print or System.out.println you cannot format the output, but in System.out.printf(); you can format the output as like this problem.
    Now if you want to change the output for example into 2 decimal place.... then you will use
    ("%.2f", Math.PI); rather than ("%.3f" , Math.PI).....

    Whatever number you put after the %. sign..( it's the remainder sign & fullstop just after the % sign) and the type like decimal or floating or double value..for integer you put %d and for floating and double you put (%.f) .
    if the value is floating or double value then you will have to use (%.2f) but if its an ineger value then you will have to use (%d) { no full stop after the % sign for an integer value )} rather than (%.f){ full stop after % sign if its double for floating numer}

    then output into that much decimal place... now if you choose to 5 decimal place then you will have to put ("%.5f" , Math.PI);
    now you don't have to use the System.out.printf to format the output.. i just showed this to tell the difference of System.out.print or println with System.out.printf (); method to show the output..

    you can use this (%.2f" to any case, it will just format the decimal number into whatever number you put after the ("%. f" , Math.PI) sign.

    Hope this answers your question
    Last edited by andaji; March 14th, 2010 at 12:45 AM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Limiting decimal places in a double

    You can use the DecimalFormat object at described in the following link:
    Double Formatting

Similar Threads

  1. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  2. float to 2 decimal places
    By fh84 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2009, 11:27 AM
  3. Double Buffering
    By Ganezan in forum Java Theory & Questions
    Replies: 2
    Last Post: November 20th, 2009, 03:51 AM
  4. string to double
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2009, 10:47 PM
  5. [SOLVED] How to use decimal place when formatting an output?
    By napenthia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 27th, 2009, 03:17 AM