# Java Change Maker need help with rounding up the pennies

• February 2nd, 2013, 03:17 PM
sxl4
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
• February 2nd, 2013, 03:24 PM
Norm
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.
• February 2nd, 2013, 03:58 PM
sxl4
Re: Java Change Maker need help with rounding up the pennies

Code java:

```/** * 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
• February 2nd, 2013, 04:27 PM
Norm
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.
• February 2nd, 2013, 04:51 PM
sxl4
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?
• February 2nd, 2013, 04:57 PM
Norm
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
• February 2nd, 2013, 05:06 PM
sxl4
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
• February 2nd, 2013, 05:11 PM
Norm
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.
• February 2nd, 2013, 06:03 PM
sxl4
Re: Java Change Maker need help with rounding up the pennies
i think this is what you mean if not it works :)

Code java:

```/** * 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
• February 2nd, 2013, 06:33 PM
Norm
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.
• February 2nd, 2013, 08:36 PM
sxl4
Re: Java Change Maker need help with rounding up the pennies
im not understanding exactly what you mean exactly? im a noob to this
• February 3rd, 2013, 06:19 AM
Norm
Re: Java Change Maker need help with rounding up the pennies
Convert dollars in double to cents in int:
Code :

`int val = (int)(doubleVal*100);`