I'm a bit confused here. This is only the second java (or any) program that I've written. It's supposed to take the amount of money you input and give you the least amount of bills and coins to use with it. It works flawlessly for anything under $32. For some reason, after that, or at least as far as I've tested, I get 1 less penny than I should, or in the case of something ending with 5 cents, I get 4 pennies instead of a nickel. I'm not really sure what could be happening, but I can only attribute it to the mod operator.. perhaps? Any help would be nice.
Here is the code I have (name has been changed :P) :
//**************************************************
// LessBills.java
// by &^&^%^&$%&(
//
// Determines the least amount of each coin and bill
// that can represent a given monetary amount
//**************************************************
import java.util.Scanner;
public class LessBills
{
public static void main (String[] args)
{
double money;
int tens, fives, ones, quarters, dimes, nickels, pennies, dollars, cents, extra;
// 'extra' is a variable that will be used to store remainders.
Scanner scan = new Scanner (System.in);
System.out.print ("How much money do you have? $");
money = scan.nextDouble();
// Convert double 'money' into individual integers for 'dollars' and 'cents'.
dollars = (int) money;
cents = (int) ((money - dollars) * 100);
// Get minimum number of bills, starting with tens and going to smaller bills.
tens = dollars / 10;
extra = dollars % 10;
fives = extra / 5;
extra = extra % 5;
ones = extra;
// Get the minimum number of coins, starting with quarters and going to smaller coins.
quarters = cents / 25;
extra = cents % 25;
dimes = extra / 10;
extra = extra % 10;
nickels = extra / 5;
extra = extra % 5;
pennies = extra;
// Print results.
System.out.println ("These are the minimum amounts of bills and coins you can keep that money in:");
System.out.println ("" + tens + " ten dollar bills. \n" + fives + " five dollar bills. \n" + ones +
" one dollar bills. \n" + quarters + " quarters. \n" + dimes + " dimes. \n" +
nickels + " nickels. \n" + pennies + " pennies.");
System.out.println ("Have a nice day.");
}
}
Posts
Doubles are notoriously horrible for representing currency values. Rounding errors and the like because binary can't super accurately represent certain floating point values. I'm thinking what's happening there is the results of the 'money' variable's representation (in the underlying binary) is 32.84999999999 instead of 32.85 or something and when you trunc it by casting to int it's just cutting off the 9999s. You could java.util.Math.round() it...
OP, is this for class or your own learning? If it's for class you might want to ask the teacher how you should procede. If this is for yourself, go with the Math.round() route.
XBL Michael Spencer || Wii 6007 6812 1605 7315 || PSN MichaelSpencerJr || Steam Michael_Spencer || Ham NOØK
QRZ || My last known GPS coordinates: FindU or APRS.fi (Car antenna feed line busted -- no ham radio for me X__X )
I don't know java data types, but it's sounding like double must be a 16-bit representation with a range of +/- 32767 (with the decimal point somewhere in there)
You might try using an integer for money, and asking the user to enter it without a decimal point (i.e. in cents), or using a string, breaking it at the decimal, and finding the values of each part to get your original dollars and cents values.
import java.math.BigDecimal should work.
http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigDecimal.html
took out her barrettes and her hair spilled out like rootbeer
Unless one is working with huge amounts of money or fractions of pennies, there's no need to use something like this. It suffices to store the amount of money as an int representing the number of cents. For instance, $12.77 gets stored as the int 1277.
Depending on your relationship with the teacher and their attitude I would even show them this page on sun's website that specifically says not to use Double for currency.