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

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

1. ## 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. ## 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. ## Re: How to set value of double to 2 decimal places

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

4. ## Re: How to set value of double to 2 decimal places

Shouldn't cent be: 92.99999999999997
That is a dollar amount: 0.9299999999999997

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

tonynsx (November 19th, 2013)

6. ## Re: How to set value of double to 2 decimal places

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.

7. 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. ## Re: How to set value of double to 2 decimal places

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.

```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. ## Re: How to set value of double to 2 decimal places

Originally Posted by tonynsx

```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. ## Re: How to set value of double to 2 decimal places

Thanks, Math.round() worked!

13. Welcome