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: Java Change Maker need help with rounding up the pennies

1. ## Java Change Maker need help with rounding up the pennies

So im new to programing and need help solving my program because my teacher never taught us about how to solve the issue. it works and compiles as of right now but if you run it. the issue is when i input for ex. .92 cents for the total bill and i input 1 dollar for tendered i need 8 cents but i only get 1 nickel and 2 pennies. she said because it rounds down the last penny.

/**
* Name: Salvatore LoCricchio
* Date January 22, 2013
* Class/Section: CIT160-03
* Problem: Calcualte change
*
*
* Sample Input: Enter bill total for .92 cents
* Enter tendered in dollars 1 dollar
*
* Sample Output: Change = 8 cents
*/
import java.util.Scanner;

public class Locricchiochangmaker
{
public static void main(String args[])
{
float spent, tendered;
int dollars,quarters, dimes, nickels, pennies,change;

Scanner keyboard = new Scanner(System.in);

System.out.println("Bill Total");
spent = keyboard.nextFloat( );
System.out.println("Tendered.");
tendered = keyboard.nextFloat( );

change = (int) ((tendered - spent)*100);

dollars = (change/100);
change = change%100;
quarters = (change/25);
change = change%25;
dimes = (change/10);
change = change%10;
nickels = (change/5);
change = change%5;
pennies = (change);

System.out.println(dollars + " dollars");

System.out.println(quarters + " quarters");

System.out.println(dimes + " dimes");

System.out.println(nickels + " nickels");

System.out.println(pennies + " pennies");
}//end main
}//end class

2. ## Re: Java Change Maker need help with rounding up the pennies

Can you copy the contents of the console window and paste it here that shows the problem?

Convert the amounts to cents before doing any math with them.

On Windows: To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

[code=java]
[/code]
to get highlighting and preserve formatting.

3. ## Re: Java Change Maker need help with rounding up the pennies

```/**
* Name: Salvatore LoCricchio
* Date January 22, 2013
* Class/Section: CIT160-03
* Problem: Calcualte change
*
*
* Sample Input: Enter bill total for .92 cents
* 				 Enter  tendered in dollars 1 dollar
*
* Sample Output: Change = 8 cents
*/
import java.util.Scanner;

public class Locricchiochangmaker
{
public static void main(String args[])
{
float spent, tendered;
int dollars,quarters, dimes, nickels, pennies,change;

Scanner keyboard = new Scanner(System.in);

System.out.println("Bill Total");
spent = keyboard.nextFloat( );
System.out.println("Tendered.");
tendered = keyboard.nextFloat( );

change =  (int) ((tendered - spent)*100);

dollars = (change/100);
change = change%100;
quarters = (change/25);
change = change%25;
dimes = (change/10);
change = change%10;
nickels = (change/5);
change = change%5;
pennies = (change);

System.out.println(dollars + " dollars");

System.out.println(quarters + " quarters");

System.out.println(dimes + " dimes");

System.out.println(nickels + " nickels");

System.out.println(pennies + " pennies");
}//end main
}//end class```

Bill Total
.92
Tendered.
1
0 dollars
0 quarters
0 dimes
1 nickels
2 pennies

4. ## Re: Java Change Maker need help with rounding up the pennies

Did you see this:
Convert the amounts to cents before doing any math with them.

5. ## Re: Java Change Maker need help with rounding up the pennies

What do you mean? Convert to cents before I put it through the this:

dollars = (change/100);
change = change%100;
quarters = (change/25);
change = change%25;
dimes = (change/10);
change = change%10;
nickels = (change/5);
change = change%5;

And how would I go about that if I'm wrong?

6. ## Re: Java Change Maker need help with rounding up the pennies

Convert the contents of these to cents BEFORE doing any math: tendered and spent

7. ## Re: Java Change Maker need help with rounding up the pennies

i think that its done here:
change = (int) ((tendered - spent)*100);

where whats left is converted to pennies

8. ## Re: Java Change Maker need help with rounding up the pennies

That does the multiply and cast AFTER the subtraction. Do it BEFORE any math operations.

Print out the value of change from that expression to see.

9. ## Re: Java Change Maker need help with rounding up the pennies

i think this is what you mean if not it works

```/**
* Name: Salvatore LoCricchio
* Date January 22, 2013
* Class/Section: CIT160-03
* Problem: Calcualte change
*
*
* Sample Input: Enter bill total for .92 cents
* 				 Enter  tendered in dollars 1 dollar
*
* Sample Output: Change = 8 cents
*/
import java.util.Scanner;

public class Locricchiochangmaker
{
public static void main(String args[])
{
float spent, tendered;
int dollars,quarters, dimes, nickels, pennies,change = 0;

Scanner keyboard = new Scanner(System.in);

System.out.println("Bill Total");
spent = keyboard.nextFloat( );
System.out.println("Tendered.");
tendered = keyboard.nextFloat( );

spent = (spent*100);
tendered = (tendered*100);

change =  (int) (tendered - spent);

dollars = (change/100);
change = change%100;
quarters = (change/25);
change = change%25;
dimes = (change/10);
change = change%10;
nickels = (change/5);
change = change%5;
pennies = (change);

System.out.println(dollars + " dollars");

System.out.println(quarters + " quarters");

System.out.println(dimes + " dimes");

System.out.println(nickels + " nickels");

System.out.println(pennies + " pennies");
}//end main
}//end class```

my out put is this :

Bill Total
.92
Tendered.
1

0 dollars
0 quarters
0 dimes
1 nickels
3 pennies

10. ## Re: Java Change Maker need help with rounding up the pennies

Convert each of the variables contents to cents in an int variable BEFORE subtracting.

11. ## Re: Java Change Maker need help with rounding up the pennies

im not understanding exactly what you mean exactly? im a noob to this

12. ## Re: Java Change Maker need help with rounding up the pennies

Convert dollars in double to cents in int:
`int val = (int)(doubleVal*100);`