# Thread: Java help, if possible

1. I'm working on my first java program and I'm stuck. I have to write a code that takes a dollar amount due and subtracts from the dollar amount paid (ie, 3.82 due and 4.00 paid = 4.00-3.82), then I have display the amount of change.

So, I'd have to calculate how many quarters, nickles, dimes and pennies. It's always less than \$1 in change being given back.

I have the math part figured out and most of the coding, I just cannot figure out how to put it together. I have to store the dollar amounts as double data types (3.82 and 4.00) but I need to display the change in integers. I cannot figure out how to go from double to integer.

I did this same project in C and it was simple, but this part I just cannot figure out in java because it's keeping everything as double since the amount paid and amount owed are both double data types.

I tried multiplying the change * 100 so at least I can use integers but I cannot store it as an integer so there are always decimals at the end and my answers return decimals (like 3.5 quarters or 4.22222 dimes, etc.)

here's what I have so far:
Code:
```import java.util.Scanner;

public class Assignment1
{
public static void main( String [] args )
{
Scanner scan = new Scanner( System.in );

final double PENNIES = 1;
final double NICKLES = 5;
final double DIMES = 10;
final double QUARTERS = 25;

System.out.println( "Enter the total amount due> ");
double total = scan.nextDouble();
System.out.println( "Enter amount paid> ");
double paid = scan.nextDouble();

double change = paid - total;
change = change * 100;

double quartersChange = change / QUARTERS;
System.out.println( "You will receive " + quartersChange + " quarters" );
}
}```
It returns a value of 2.9600000000000013 quarters, but I want it to drop the remainder and just use "2" as the value.

any help would be appreciated.  2. Hello!

There are a lot of ways to do that

the first is: cast your result to an Integer

double quartersChange = change / QUARTERS;
try to put
int quartersChange = (int) (change / QUARTERS);

the second is: get the floor/ceil of your number, like that:
double quartersChange = Math.floor( change / QUARTERS );
double quartersChange = Math.ceil( change / QUARTERS );

you can make a BigDecimal object and format its scale, like that:
BigDecimal myNumber = new BigDecimal( change / QUARTERS );
myNumber.setScale( 2 ); // with 2 decimal scales
// I dunno if it really works, but try :P

finally you can make a DecimalFormat
DecimalFormat myFormater = new DecimalFormat( "0.00" );
System.out.println( myFormater.format( change / QUARTERS );

try these examples and tell me if all worked ok

see ya dude  Posting Permissions
 You may not post new threads You may not post replies You may not post attachments You may not edit your posts   BB code is On Smilies are On [IMG] code is On [VIDEO] code is On HTML code is Off Trackbacks are On Pingbacks are On Refbacks are On Forum Rules