How would I go about formatting the println in this code? The typical printf isn
ID: 658582 • Letter: H
Question
How would I go about formatting the println in this code? The typical printf isn't working. I need the numbers to have 2 decimal places.
Account Mike = new Account("Mike",500);// Create a new object from Account class.
System.out.println(Mike.toString());//Get account information.
System.out.println("Mike deposits $500.");
Mike.Deposit(500); //Make a deposit in amount of 500.
System.out.println("Account Balance is $" + Mike.Inquiry());//Print new balance.
Account Susan = new Account("Susan",100); //Create a new object from Account class.
System.out.println(" " + Susan.toString());//Get account information.
System.out.println("Susan withdraws $50.");
Susan.Withdraw(50);//Withdraw $50 from account Susan.
System.out.println("Account Balance is $" + Susan.Inquiry());//Print new balance.
System.out.println("Susan tries to withdraw $70.");
Susan.Withdraw(70); //Try to withdraw $70 from account Susan.
This is the current output:
Name: Mike Balance: $500.0 Account Number: 8655.
Mike deposits $500.
Account Balance is $1000.0
Name: Susan Balance: $100.0 Account Number: 7027.
Susan withdraws $50.
Account Balance is $50.0
Susan tries to withdraw $70.
Insufficient funds
Explanation / Answer
Float numbers to be print in two decimal places -
System.out.println(String.format("%.2f",12.454512154))
or
System.out.printf("%.2f", 12.1564561)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.