0

I want my Java program to round numbers off to two decimals. I've been looking at my class materials and I can't find a solution. Here's the code

import java.io.*;

import java.util.*;

public class prog58i

{
public static void main (String[] args)

{
//input
System.out.print("the amount I wish to borrow is? ");
Scanner a = new Scanner(System.in);
double borrow = a.nextDouble();
//Shows amount of money someone wants to borrow

System.out.print("The loan rate I can get is? ");
Scanner b = new Scanner(System.in);
double rate = b.nextDouble();
//Shows loan interest rate

System.out.print("The number of months it will take me to pay of this loan is? ");
Scanner c = new Scanner(System.in);
double months = c.nextDouble();
//Shows months loan will be taken out


//The Math Part
double MP = borrow * (rate/1200) * (Math.pow((1+rate/1200), months)) /     (Math.pow((1+rate/1200), months)- 1);
double intrest = (MP * months) - borrow;
double repaid = (MP*months);

//Output
System.out.print( "My monthly payments will be $" +MP +"\n");
System.out.print( "Total Interest Paid is $" +intrest +"\n");
System.out.print( "Total Amount Paid is $" +repaid +"\n");
}
}

I'm more looking for a tutorial or just some advice on how to do it myself.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 2
    Unreadable - pay more attention to formats and variable names. Those things matter for non-trivial, non-student code. Might as well get into the habit now. – duffymo Oct 06 '15 at 19:55

1 Answers1

1

if you just need to round the output, you can do

System.out.printf("My monthly payments will be $%.2f%n", MP);
System.out.printf("Total Interest Paid is $%.2f%n", intrest);
System.out.printf("Total Amount Paid is $%.2f%n", repaid);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Wait wait wait, after using your code the output says Total Amount Paid is just $2.f instead of the actual output. https://gyazo.com/beb0591a3e200d67cdea471be0705714 EDIT: Fixed it – Andrew Webb Oct 06 '15 at 20:00
  • @AndrewWebb I left one % out to see if you could figure it out ;) I have fixed it now. – Peter Lawrey Oct 06 '15 at 21:27