java - Error with Random.nextDouble math -
I am working on a small app that has to deposit and withdraw money from a bank account. This program is mostly going well, but I am getting serial numbers from many transactions, in which there are many trailing decimals. I am randomly producing doubles, which should only have 2 decimals, so I'm not sure where they are coming from.
c: \ user \ John \ document \ class \ java & gt; Java Savings Account Balance Balance: 62.0 Deposit Number: 0 Word Count: 0 Annual Interest Rate: 0.09 Service Charges: 0.0 Active: Submit the wrong first return, then randomly generated amount. Balance: 62.0 Forcibly: Deposit: 8.76 35.43 Your account is inactive: Inadequate fund *** unable to complete the withdrawal. *** Remaining: 97.43 per day: Deposit: 16.83 3.98 Balance: 84.58000000000001 With: Deposit: 99.44 35.2 Inactive due to your account: Inadequate fund *** Unable to complete the withdrawal. *** Submit to continue C: \ Users \ John \ Documents \ Class \ Java & gt;
This issue is with another transaction, beginning with the third group of transactions with the balance of 84.58 billion
Here is the code for my main category.
> Import java.util.Random; Import java.util.Scanner; Public Segment SavingsCountTest {Public Fixed Zero Main (String [] Args) {Random Rand = New Random (); Double [] deposits = {(double) rand. NXITINT (10001) / 100, (Double) Rand NXITI (10001) / 100, (double) rand NXITINT (10001) / 100}; Double [] back come = {(double) rand. NXT (10001) / 100, (double) RAND. NXIT (10001) / 100, (double) rand NXITINT (10001) / 100}; Savings Account Account = New Savings Account ((Double) Rand.NextTint (101), (Double) (Rand NXTint (10) +1) / 100.0); System.out.print (account.toString ()); System.out.println ("\ n \ n \ tBest first, then \ n \ trandomly submit the amount generated."); {System.out.println ("\ n Balance:" + account.getBalance ()) for (Int i = 0; I & lt; 3; i ++); System.out.print ("Withrdaw: \ t \ tDeposit: \ n" + Take back [i] + "\ t \ t \ t" + plus array [i] + "\ n \ n"); Account.withdraw (withdrawArray [i]); Account.deposit (depositArray [i]); } Break (); } Private static zero break () {scanner keyboard = new scanner (System.in); System.out.print ("\ nPress enter to continue"); Keyboard.nextLine (); System.out.print ("\ n"); }}
Here are the deposits and withdraw the methods of the savings account
Public invalid withdrawals (double amount) {if (this active & amp; amount & Lt; = super.balance) super.withdraw (quantity); Else System.out.print ("Your account is inactive:" + "Insufficient funds" + "\ n ***" Unable to complete ". *** \ n"); } Public Zero Deposit (dual amount) {if (super.balance + zodiac> = 25.00) {this.active = true; Super.deposit (amount); } And superdetapet (zodiac); }
This is a deposit and withdraw the methods of BankAntity Superclass:
Public invalid return (double return) {this.balance - = withdraw ; WithdrawCount ++; } Public Zero Deposit (Double Deposit) {this.balance + = Deposit; DepositCount ++; }
That's because the value is stored in binary 84.58 can not be represented Absolutely in the binary, but it can get close apparently, it can get around 84.58 billion billion dollars, to correct it, you can use the format to format.
Comments
Post a Comment