2

The System.printf("%.2f", currentBalance) is working fine but the problem is the rounded number appears after the sentence. Put the code into your eclipse program and run it and you can see something is definitely wrong. If someone could help it would be much appreciated.

public class BankCompound {


public static void main (String[] args) {
    compound (0.5, 1500, 1);
}

public static double compound (double interestRate, double currentBalance, int year) {

    for (; year <= 9 ; year ++) {

    System.out.println ("At year " + year +  ", your total amount of money is ");
    System.out.printf("%.2f", currentBalance);
    currentBalance = currentBalance + (currentBalance * interestRate);  
    }
    System.out.println ("Your final balance after 10 years is " + currentBalance);
    return currentBalance;
} 

}

user1718272
  • 39
  • 1
  • 1
  • 6

4 Answers4

2

Please try this

import java.text.DecimalFormat;



public class Visitor {


    public static void main (String[] args) {
        compound (0.5, 1500, 1);
    }

    public static double compound (double interestRate, double currentBalance, int year) {

        for (; year <= 9 ; year ++) {

        System.out.println ("At year " + year +  ", your total amount of money is "+Double.parseDouble(new DecimalFormat("#.##").format(currentBalance)));


        currentBalance = currentBalance + (currentBalance * interestRate);  
        }
        System.out.println ("Your final balance after 10 years is " + currentBalance);
        return currentBalance;
    } 
}
sunleo
  • 10,589
  • 35
  • 116
  • 196
  • 1
    DecimalFormat is the way to go. If you're using the same format over and over, make your DecimalFormat a static final and re-use it for efficiency. – AWT Oct 28 '12 at 02:27
1

System.out.println(), as the name implies

behaves as though it invokes print(String) and then println().

Use System.out.print() and put the newline after printing the current balance.

System.out.print("At year " + year +  ", your total amount of money is ");
System.out.printf("%.2f", currentBalance);
System.out.println();

// or
System.out.print("At year " + year +  ", your total amount of money is ");
System.out.printf("%.2f\n", currentBalance);
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0

System.out.printf ("At year %d, your total amount of money is %.2f\n", year, currentBalance);

Darren
  • 722
  • 4
  • 12
0

Faulty call is the very first System.out.println() as it appends a new line after printing the given content.

There are two solutions-

Approach -1 :

System.out.print("At year " + year +  ", your total amount of money is ");
System.out.printf("%.2f\n", currentBalance);

Approach -2 : [Using String.format() with println()]

System.out.println ("At year " + year + ", your total amount of money is "
                                      + String.format("%.2f", currentBalance));

Both will produce the same result. Even the second one is more readable.

Output:

At year 1, your total amount of money is 1500.00

At year 2, your total amount of money is 2250.00

At year 3, your total amount of money is 3375.00

At year 4, your total amount of money is 5062.50

At year 5, your total amount of money is 7593.75

At year 6, your total amount of money is 11390.63

At year 7, your total amount of money is 17085.94

At year 8, your total amount of money is 25628.91

At year 9, your total amount of money is 38443.36

Your final balance after 10 years is 57665.0390625

String.format returns a formatted string. System.out.printf also prints the formatted string on system.out(console).

Use them as per your needs.

Abhishek
  • 422
  • 4
  • 13