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

Thread: How to set value of double to 2 decimal places

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default How to set value of double to 2 decimal places

    How can I make the value of double 2 decimal places? I know how to display 2 decimal places, but how to actually set the values to 2?

    Example, my change below is 7.93

    So it knows the dollar is 7, but for the cent, it's showing 0.9299999999999997 instead of .93

    double change = 7.93;
     
    int dollar;
    double cent;
    dollar = (int)change % 1000000;
    cent = change - dollar;
     
    System.out.println("Dollar " + dollar);
    System.out.println("Cent " + cent);

    Dollar 7
    Cent 0.9299999999999997


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to set value of double to 2 decimal places

    Representing money.

    If you must use double, look at printf() for formatting output and explore the topic "rounding" for conditioning the values to be what you want.

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: How to set value of double to 2 decimal places

    Quote Originally Posted by GregBrannon View Post
    Representing money.

    If you must use double, look at printf() for formatting output and explore the topic "rounding" for conditioning the values to be what you want.
    Thank you

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to set value of double to 2 decimal places

    Shouldn't cent be: 92.99999999999997
    That is a dollar amount: 0.9299999999999997
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    tonynsx (November 19th, 2013)

  6. #5
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: How to set value of double to 2 decimal places

    Quote Originally Posted by Norm View Post
    Shouldn't cent be: 92.99999999999997
    That is a dollar amount: 0.9299999999999997
    If I have, 77.92, the 77 is the dollar, then .92 is the cent. Unless my calculations are wrong above.

  7. #6
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    You misunderstood what Norm was saying.

    Cents should be an integer. Do I give you 92 cents or .92 of a cent?

  8. The Following 2 Users Say Thank You to Kewish For This Useful Post:

    Norm (November 19th, 2013), tonynsx (November 19th, 2013)

  9. #7
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: How to set value of double to 2 decimal places

    Quote Originally Posted by Kewish View Post
    You misunderstood what Norm was saying.

    Cents should be an integer. Do I give you 92 cents or .92 of a cent?
    Thanks for clearing that up, yes I did misunderstood.

    --- Update ---

    Now it's working the way I want. Please let me know if I can improve on anything.

    Change: 88.38, Dollar: 88, getCent: 37
    1 $50
    1 $20
    1 $10
    1 $5
    3 $1
    1 $0.25
    1 $0.10
    2 $0.01

    package correctchange;
     
    public class CorrectChange {
     
        public static void main(String[] args) {          
     
            double change = 88.38;
     
            int dollar,cent;
            double getCent;
            dollar = (int)change % 1000000;
            getCent = (change - dollar) * 100;
            cent = (int)(getCent);
     
            System.out.println("Change: " + change + ", Dollar: " + dollar + ", getCent: " + cent);
     
            // dollar calculation
     
            if((dollar / 100) >= 1)
            {
                System.out.println(dollar / 100 + " $100");
                dollar = dollar - 100 * (dollar / 100);
            }
     
            if((dollar / 50) >= 1)
            {
                System.out.println(dollar / 50 + " $50");
                dollar = dollar - 50 * (dollar / 50);
            }
     
            if((dollar / 20) >= 1)
            {
                System.out.println(dollar / 20 + " $20");
                dollar = dollar - 20 * (dollar / 20);
            }
     
            if((dollar / 10) >= 1)
            {
                System.out.println(dollar / 10 + " $10");
                dollar = dollar - 10 * (dollar / 10);
            }
     
            if((dollar / 5) >= 1)
            {
                System.out.println(dollar / 5 + " $5");
                dollar = dollar - 5 * (dollar / 5);
            }
     
            if((dollar / 1) >= 1)
            {
                System.out.println(dollar / 1 + " $1");
            }
     
            // getCents calculation     
     
            if((cent / 25) >= 1)
            {
                System.out.println((cent / 25) + " $0.25");
                cent = cent - 25 * (cent / 25);
            }
     
            if((cent / 10) >= 1)
            {
                System.out.println((cent / 10) + " $0.10");
                cent = cent - 10 * (cent / 10);
            }
     
            if((cent / 5) >= 1)
            {
                System.out.println((cent / 5) + " $0.05");
                cent = cent - 5 * (cent / 5);
            }
     
            if((cent / 1) >= 1)
            {
                System.out.println((cent / 1) + " $0.01");
            }
     
        }
     
    }


    --- Update ---

    Just found another problem. I gave test value of 19.83. But. Instead of 83 it's showing 82 for cents

  10. #8
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: How to set value of double to 2 decimal places

    Quote Originally Posted by tonynsx View Post

    getCent = (change - dollar) * 100;
    cent = (int)(getCent);

    Just found another problem. I gave test value of 19.83. But. Instead of 83 it's showing 82 for cents
    That is because your double is being truncated and not rounded. You need to think about how you will handle your rounding before casting to an integer.

    What is the value of (change - dollar) * 100; before you typecast? Use a print statement. It will become obvious what is going on. Handle your rounding then typecast.

    Hint: Math Class

    Also, what is the theory behind the % 1 Million? What if the change is 1,000,001.83? How many dollars will you have?

  11. The Following User Says Thank You to Kewish For This Useful Post:

    tonynsx (November 20th, 2013)

  12. #9
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: How to set value of double to 2 decimal places

    Thanks, Math.round() worked!

  13. #10
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    Welcome

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. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  3. Limiting decimal places in a double
    By darek9576 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 14th, 2010, 11:27 AM
  4. float to 2 decimal places
    By fh84 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2009, 11:27 AM
  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