Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Visual J# / June 2004

Tip: Looking for answers? Try searching our database.

MoneyCount

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Janie - 05 Jun 2004 07:06 GMT
Can anyone tell me why this code doesn't work right on some number
import java.io.*;

class countmoney
 public static void main (String[] args) throws IOException

   BufferedReader stdin = new BufferedReade
     (new InputStreamReader(System.in))
    // declares three integer
   double  amount, sum;// declares a number that can have decimal

   System.out.print ("Amount? ")
   System.out.flush();

   // read a line, and then converts it to an intege
  amount=Double.parseDouble(stdin.readLine())

 
    int remainingAmount = (int)(amount*100)

   // Find the number of one dollar

   int numOfOneDollars = remainingAmount/100

   remainingAmount = remainingAmount%100

   // Find the number of quaters in the remaining amoun

   int numOfQuarters = remainingAmount/25

   remainingAmount = remainingAmount%25

   // Find the number of dimes in the remaining amoun

   int numOfDimes = remainingAmount/10

   remainingAmount = remainingAmount%10

   // Find the number of nickels in the remaining amoun

   int numOfNickels = remainingAmount/5

   remainingAmount = remainingAmount%5

   // Find the number of pennies in the remaining amoun

   int numOfPennies = remainingAmount

   // Display result

 
   if (numOfOneDollars == 1)
           System.out.println(numOfOneDollars + "--Dollar")
       
else if (numOfOneDollars > 1)
           System.out.println(numOfOneDollars + "--Dollars")
       
   if (numOfQuarters == 1)
           System.out.println(numOfQuarters + "--Quarter")
       
else if (numOfQuarters > 1)
           System.out.println(numOfQuarters + "--Quarters")
       
   if (numOfDimes == 1)
           System.out.println(numOfDimes + "--Dime")
       
else if (numOfDimes > 1)
           System.out.println(numOfDimes + "--Dimes")
       

   if (numOfNickels == 1)
           System.out.println(numOfNickels + "--Nickel")
       
else if (numOfNickels > 1)
           System.out.println(numOfNickels + "--Nickels")
       

  if (numOfPennies == 1)
           System.out.println(numOfPennies + "--penny")
       
else if (numOfPennies > 1)
           System.out.println(numOfPennies + "--pennies")
       
       

 } // method mai
}
It doesn't work on 29 cents it say it has 1 quarter and 3 pennies.  57 cents says 2 quarters 1 nickel and 1 penny.  58 cents has 2 quarters 1 nickel and 2 pennies.  1.13 has 1 dollar 1 dime and 2 pennies.  1.14 has 1 dollar 1 dime and 3 pennies.   1.15 has 1 dollar 1 dime and 4 pennies.  1.16 has 1 dollar 1 dime and 1 nickel.  Does anyone see a pattern here?
Lars-Inge T?nnessen - 05 Jun 2004 14:47 GMT
Hi Janie,

Please use float instead of double.   ("float" and "int" are both 32 bits, "double" is 64 bits in Java. These are the sizes the
numbers "occupy" inside the computer)

float amount = (float)0.29;

>     double  amount, sum;// declares a number that can have decimals
>    amount=Double.parseDouble(stdin.readLine());

Regards,
Lars-Inge T?nnessen
www.larsinge.com
Janie - 05 Jun 2004 19:46 GMT
thank you so much you are so great all of you
Bruno Jouhier [MVP] - 05 Jun 2004 18:17 GMT
This is because you are using "floating point" numbers.
Floating point numbers represent powers of 2 (even negative ones) exactly
but don't represent all powers of 10 exactly.
So, when you write:
   double amount = 0.29;
amount is not "exactly" 0.29, it is close to 0.29 - 3.55e-17.

To test this, you can just execute:
   System.out.println("delta=" + (0.29 * 100 - 29.0));
you will get:
   delta=-3.5527136788005009E-15

So, when you execute (int)(amount * 100), you obtain 28 rather than 29
because amount * 100 is a bit less than 29, and then you get one cent less
than what you would expect.

To fix this, you can just replace the (int) cast by a call to Math.round:

   (int)(amount * 100) => 28
   Math.round(amount * 100) => 29

Bruno.

> Can anyone tell me why this code doesn't work right on some numbers
> import java.io.*;
[quoted text clipped - 83 lines]
> }
> It doesn't work on 29 cents it say it has 1 quarter and 3 pennies.  57 cents says 2 quarters 1 nickel and 1 penny.  58 cents has 2 quarters 1
nickel and 2 pennies.  1.13 has 1 dollar 1 dime and 2 pennies.  1.14 has 1
dollar 1 dime and 3 pennies.   1.15 has 1 dollar 1 dime and 4 pennies.  1.16
has 1 dollar 1 dime and 1 nickel.  Does anyone see a pattern here?
Janie - 05 Jun 2004 19:46 GMT
Bruno,  Thank you you really are a most valuable person Thanks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.