# Why is it not ROUNDING HELP PLEASE

• October 1st, 2013, 12:45 AM
quantamphysics
Why is it not ROUNDING HELP PLEASE
Okay so I am supposed to write a code which accepts a certain amount of money ( 7.53) which then outputs what coins i need to make this amount up, i try it and compile it, but its giving me the interactions shown below as decimals and not whole numbers. (obvs it doesnt look good to have 0.003765 toonies) Please HELP THANKS

import java.util.*;
import javax.swing.*;
public class CoinCounter {

public static void main (String [] args){

Scanner sc = new Scanner(System.in);
String userInput;
double amount;
int loonie;
int toonie;
int quarter;
int dime;
int nickle;
int pennie;
int totalCoins = 0;
double myCoins;

userInput = JOptionPane.showInputDialog("Please enter an amount");
amount = Double.parseDouble(userInput);
System.out.println("Enter the amount");
amount=sc.nextDouble();

System.out.println((amount / 200) + " – toonies");
myCoins = (amount / 200);
amount = amount % 200;

if(amount > 0){
for(int a = 0; a < myCoins; a++ ){
totalCoins++;
}
}

System.out.println((amount / 100) + " – loonies");
myCoins = (amount / 100);
amount = amount % 100;

if(amount > 0){
for(int a = 0; a < myCoins; a++ ){
totalCoins++;
}
}

System.out.println((amount / 25) + " – quarters");
myCoins = (amount / 25);
amount = amount % 25;

if(amount > 0){
for(int a = 0; a < myCoins; a++ ){
totalCoins++;
}
}

System.out.println((amount / 10) + " – dimes");
myCoins = (amount / 10);
amount = amount % 10;

if(amount > 0){
for(int a = 0; a < myCoins; a++ ){
totalCoins++;
}
}

System.out.println((amount / 5) + " –Nickles");
myCoins = (amount / 5);
amount = amount % 5;

if(amount > 0){
for(int a = 0; a < myCoins; a++ ){
totalCoins++;
}
}

System.out.println((amount/1) + " – pennies");
myCoins = (amount);
amount = amount % ;

if(amount > 0){
for(int a = 0; a < myCoins; a++ ){
totalCoins++;
}
}

System.out.println("Total Coins: " + totalCoins);

}
}
• October 1st, 2013, 02:03 AM
Junky
Re: Why is it not ROUNDING HELP PLEASE
One way to handle money is to use whole amounts instead of fractional amounts. By that I mean use 753 instead of 7.53. Hopefully you know what the problem is. If not Google "What every computer scientist should know about float point numbers" or something like that.

--- Update ---

Code :

```for(int a = 0; a < myCoins; a++ ){ totalCoins++; }```
I thought the objective was to work out how many of each coin denomination there is. But you never use the loonie, toonie etc variables. Also why are you reading in the amount twice? Once via a dialog and once via a Scanner.
• October 1st, 2013, 04:43 AM
GregBrannon
Re: Why is it not ROUNDING HELP PLEASE