# How to set value of double to 2 decimal places

• November 19th, 2013, 03:33 PM
tonynsx
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

Code :

```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);```

Code :

```Dollar 7 Cent 0.9299999999999997```
• November 19th, 2013, 04:19 PM
GregBrannon
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.
• November 19th, 2013, 04:22 PM
tonynsx
Re: How to set value of double to 2 decimal places
Quote:

Originally Posted by GregBrannon
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
• November 19th, 2013, 04:34 PM
Norm
Re: How to set value of double to 2 decimal places
Shouldn't cent be: 92.99999999999997
That is a dollar amount: 0.9299999999999997
• November 19th, 2013, 08:15 PM
tonynsx
Re: How to set value of double to 2 decimal places
Quote:

Originally Posted by Norm
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.
• November 19th, 2013, 08:27 PM
Kewish
You misunderstood what Norm was saying.

Cents should be an integer. Do I give you 92 cents or .92 of a cent?
• November 19th, 2013, 09:16 PM
tonynsx
Re: How to set value of double to 2 decimal places
Quote:

Originally Posted by Kewish
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.

Code :

```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```

Code :

```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
• November 19th, 2013, 10:24 PM
Kewish
Re: How to set value of double to 2 decimal places
Quote:

Originally Posted by tonynsx

Code java:

```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?
• November 20th, 2013, 07:31 AM
tonynsx
Re: How to set value of double to 2 decimal places
Thanks, Math.round() worked!
• November 20th, 2013, 03:15 PM
Kewish
Welcome