472,145 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Money program

KoreyAusTex
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
7 2853
Ganon11
3,652 Expert 2GB
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
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
1,216 Expert 1GB
Loose the doubles and use ints -- cents.
Feb 5 '08 #4
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
1,216 Expert 1GB
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
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
3,112 Expert 2GB
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.

Similar topics

1 post views Thread by =?Utf-8?B?R29vZFRyb3VibGU=?= | last post: by
reply views Thread by =?Utf-8?B?dHJvdWJsZWQgTVMgTW9uZXkgMjAwMiB1c2Vy?= | last post: by
1 post views Thread by =?Utf-8?B?bW9uZXkgdXNlciBrc2E=?= | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.