mdegges
mdegges

Reputation: 963

how to change user input from integer to decimal

I'm new to java and was wondering how to change a user input (integer) into decimal form. for ex, if the user inputs 6 for yearly interest, it will be changed to 0.06 before the calculations are done and the answer is printed.

this program is just used to determine how long a bank account will last.. but I am also supposed to print this in a year and month format. I'm not sure how to do that, except to put in another if statement and say if (answer from the above calculations > 12) subtract 12 from the calculations and add1 to year.. and somehow put that in a loop.

if anyone has advice/pointers to give on doing this it'd really be helpful!

import java.util.*;

class Two {
  public static void main(String[] args) {
    Scanner imp = new Scanner(System.in);
    System.out.print("Enter your initial balance: ");
    double bal = imp.nextDouble();
    System.out.print("Enter your yearly interest: ");
    double intr = imp.nextInt();
    System.out.print("Enter your monthly withdrawls: ");
    double wtd = imp.nextDouble();
    if (true) { //need to change 
      System.out.print("Your account will last " + (((bal*intr) + bal)/ wtd) + " months");
    } else { System.out.print("The account will last forever");

    }
  }

}

Upvotes: 1

Views: 5344

Answers (3)

Snowy Coder Girl
Snowy Coder Girl

Reputation: 5518

You want to convert to percentage, so you'll have to divide by 100. Here is a possible solution:

double intrAsDouble = ((double)intr)/100;

Oh, and as for the date thing:

int totalMonths = ((bal*intrAsDouble) + bal))/wtd;
int years = (int)(totalMonths/12);
int months = totalMonths%12;

String accountWillLast = "";

boolean hasMonths = months != 0;
boolean hasYears = years != 0;

if (hasYears) {
    accountWillLast = accountWillLast + years;
    if (years == 1) accountWillLast = accountWillLast + " year";
    else accountWillLast = accountWillLast + " years";
}
if (hasMonths && hasYears) {
    accountWillLast = accountWillLast + " and ";
}
if (hasMonths) {
    accountWillLast = accountWillLast + months;
    if (months == 1) accountWillLast = accountWillLast + " month";
    else accountWillLast = accountWillLast + " months";
}

Upvotes: 3

mre
mre

Reputation: 44240

Try

double intr = ((double)imp.nextInt()/100);

EDIT

Oops, I missed that nested question. See How to convert months to years-and-months.

Upvotes: 3

ratchet freak
ratchet freak

Reputation: 48196

you can multiply with 0.01 (this avoids the explicit cast though the conversion from int to double needs to happen somehow)

double intr = imp.nextInt()*0.01;

and to transfer months into years + months check out integer division and the modulo operator

int months = 18;//calculated

int years = month/12;//integer division is a implicitly rounded down
months = months%12;

or if you really need to make a loop

int months = 18;//calculated
int years = 0;
while(months >= 12){
    months -= 12;
    years += 1;
}

Upvotes: 4

Related Questions