0

I am getting output of my code as 10.5 which is an interest amount but I want the answer rounded upto three decimal places. Even if the result does not have decimal it should be displayed with a suffix ".000".

I answer I am getting is - 10.5 The answer I want is - 105.000

How can I do this.

Here is my code:

import java.util.Scanner;

public class Account {

    int id;
    double balance;
    double interestRate;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public double getInterestRate() {
        return interestRate;
    }
    public void setInterestRate(double interestRate) {
        this.interestRate = interestRate;
    }


    Account(int id,double balance,double interestRate)
    {
        this.id=id;
        this.balance=balance;
        this.interestRate=interestRate;
    }
}

class Solution
{
    public static void main(String args[])

    {
        Scanner sc=new Scanner(System.in);
        int id=sc.nextInt();
        double balance=sc.nextDouble();
        double interestRate=sc.nextDouble();

        Account p=new Account(id,balance,interestRate);

        int noOfYears=sc.nextInt();
        sc.close();

        double q=calculateInterest(p,noOfYears);

        System.out.println(q);
    }

        public static double calculateInterest(Account a,int noOfYears)
        {
            double x=a.interestRate*noOfYears/100;
            double y=x+a.interestRate;

            return y;


        }

}

Inputs are: 1 1000 10 5

Actual Output- 10.5 Output I want-105.000

Jens
  • 67,715
  • 15
  • 98
  • 113

0 Answers0