2015-10-06 3 views
0

Я хочу, чтобы моя программа Java округляла числа до двух десятичных знаков. Я смотрю на свои материалы, и я не могу найти решение. Вот кодКак мне заставить мою программу Java крутиться?

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"); 
} 
} 

Я больше ищу учебник или просто совет о том, как это сделать сам.

+2

Unreadable - обращать больше внимания на форматы и имена переменных. Эти вещи имеют значение для нетривиального, не-студенческого кода. Мог бы теперь войти в привычку. – duffymo

ответ

1

если вам просто нужно округлить выход, вы можете сделать

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); 
+0

О, ладно, спасибо! –

+0

Подождите, подождите, после использования вашего кода выступит с сообщением Total Amount Paid, всего $ 2.f вместо фактического вывода. https://gyazo.com/beb0591a3e200d67cdea471be0705714 EDIT: Исправлено: –

+0

@AndrewWebb Я оставил один%, чтобы увидеть, можете ли вы это понять;) Я исправил его сейчас. –

Смежные вопросы