By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,070 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,070 IT Pros & Developers. It's quick & easy.

Money program

KoreyAusTex
P: 36
I am having a very hard time making this program work right, I am sort of lost and I was wondering if I might get some advice.

This is what I have so far but I have a rounding error when I use the amount 10.03:

Expand|Select|Wrap|Line Numbers
  1. public class Money
  2.     public static void main(String[] args)
  3.     { 
  4.         double TOTAL_AMOUNT = 10.03;
  5.  
  6.         int money = (int)(TOTAL_AMOUNT * 100);
  7.  
  8.         System.out.println("Your money amount " + TOTAL_AMOUNT + " consists of");
  9.  
  10.         System.out.println("\t" + (money / 100) + " dollars");
  11.  
  12.         money = money % 100;
  13.  
  14.         System.out.println("\t" + (money / 25) + " quarters");
  15.         money = money % 25;
  16.  
  17.         System.out.println("\t" + (money / 10) + " dimes");
  18.         money = money % 10;
  19.  
  20.         System.out.println("\t" + (money / 5) + " nickels");
  21.         money = money % 5;
  22.  
  23.         System.out.println("\t" + (money / 1) + " pennies");
  24.     }
  25. }
I know Math.round() is a useful class but can't figure how to put it in my program
Feb 5 '08 #1
Share this Question
Share on Google+
7 Replies


Ganon11
Expert 2.5K+
P: 3,652
Do you need to use a floating point (decimal) number to hold the money? I see you immediately convert it to an integer - maybe you should just start with an integer.
Feb 5 '08 #2

P: 16
You're problems is that you're not using Math.round.

just replace:
int money = (int)(TOTAL_AMOUNT * 100);
with:
int money = (int)Math.round(TOTAL_AMOUNT * 100);

it should round a lot more accurately now, try it out.

hope it helps :)

I am having a very hard time making this program work right, I am sort of lost and I was wondering if I might get some advice.

This is what I have so far but I have a rounding error when I use the amount 10.03:

Expand|Select|Wrap|Line Numbers
  1. public class Money
  2.     public static void main(String[] args)
  3.     { 
  4.         double TOTAL_AMOUNT = 10.03;
  5.  
  6.         int money = (int)(TOTAL_AMOUNT * 100);
  7.  
  8.         System.out.println("Your money amount " + TOTAL_AMOUNT + " consists of");
  9.  
  10.         System.out.println("\t" + (money / 100) + " dollars");
  11.  
  12.         money = money % 100;
  13.  
  14.         System.out.println("\t" + (money / 25) + " quarters");
  15.         money = money % 25;
  16.  
  17.         System.out.println("\t" + (money / 10) + " dimes");
  18.         money = money % 10;
  19.  
  20.         System.out.println("\t" + (money / 5) + " nickels");
  21.         money = money % 5;
  22.  
  23.         System.out.println("\t" + (money / 1) + " pennies");
  24.     }
  25. }
I know Math.round() is a useful class but can't figure how to put it in my program
Feb 5 '08 #3

BigDaddyLH
Expert 100+
P: 1,216
Loose the doubles and use ints -- cents.
Feb 5 '08 #4

KoreyAusTex
P: 36
Loose the doubles and use ints -- cents.
My professor wanted it in the format of 10.03 or 15.34. She didn't let us break it up, if I understand you correctly?
Feb 9 '08 #5

BigDaddyLH
Expert 100+
P: 1,216
My professor wanted it in the format of 10.03 or 15.34. She didn't let us break it up, if I understand you correctly?
Well, I'm not sure to say since I don't know the whole story, but if I were writing the program for my own benefit, I would work with ints, and merely format the output to look "15.34".

If you have time, ask your professor to clarify things for you.
Feb 9 '08 #6

P: 1
This is very helpful. But what if the output is 0 and you don't want it to show in the results?
Jul 9 '13 #7

Nepomuk
Expert 2.5K+
P: 3,112
You can always catch specific cases with an if branch. When working with integers, you'd simply put something like this:
Expand|Select|Wrap|Line Numbers
  1. if(money / 25 > 0) {
  2.     System.out.println("\t" + (money / 25) + " quarters");
  3. }
You could even use an if-else structure to give a more natural response:
Expand|Select|Wrap|Line Numbers
  1. if(money / 25 > 0) {
  2.     System.out.println("\t" + (money / 25) + " quarters");
  3. } else {
  4.     System.out.println("\tNo quarters");
  5. }
There are shorter ways of doing this, but this is the easiest to understand.
Jul 9 '13 #8

Post your reply

Sign in to post your reply or Sign up for a free account.