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 4 of 4

Thread: Java program to format a double value to 2 decimal places

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Post Java program to format a double value to 2 decimal places

    This code allows you to round a double value to 2 decimal places using DecimalFormat.

    You can increase the decimal places by adding an extra #. For example DecimalFormat("#.###"); rounds to 3 decimal places.

    import java.text.*;
     
    public class DecimalPlaces {
     
        public static void main(String[] args) {
     
            double d = 1.234567;
            DecimalFormat df = new DecimalFormat("#.##");
            System.out.print(df.format(d));
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  2. The Following 4 Users Say Thank You to JavaPF For This Useful Post:

    copeg (February 9th, 2010), sentmca (June 28th, 2010), Shambolic (February 9th, 2010), YourCrazyFriend (March 20th, 2012)


  3. #2
    Junior Member newProgrammer's Avatar
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Format a double value to 2 decimal places

    Thank you very much!

  4. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: How to Format a double value to 2 decimal places

    you can also do this,

    public class DecimalPlaces {
     
        public static void main(String[] args) {
     
            double d = 1.234567;
            System.out.printf("%1$.2f", d);
        }
     
    }

  5. #4
    Junior Member
    Join Date
    May 2010
    Location
    Kurunegala, SriLanka
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to Format a double value to 2 decimal places

    public void GetTwoDecimal(){
     
            double d = 2.34568;
            DecimalFormat f = new DecimalFormat("##.00");  // this will helps you to always keeps in two decimal places
            System.out.println(f.format(d)); 
    }
    Last edited by helloworld922; December 4th, 2010 at 05:16 PM.

  6. The Following User Says Thank You to madushankakurera For This Useful Post:

    keepStriving (December 3rd, 2013)

Similar Threads

  1. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  2. [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

Tags for this Thread