0
這裏是我必須回答的問題: (比較各種利率的貸款)編寫一個程序,讓用戶輸入貸款數量和貸款期限以年數顯示每個月的利息支付總額從5%到8%不等,增加1/8。小數點後的位數和固定列格式化
import java.util.Scanner; // Scanner is in java.util
public class FinancialApp {
\t public static void main(String[] args) {
\t Scanner input = new Scanner(System.in); // Scanner object
\t System.out.print("Enter loan amount: "); // input value for loan
\t \t double loanAmount = input.nextDouble();
\t System.out.print("Enter number of years as an integer: ");
\t \t int years = input.nextInt();
\t System.out.println("Intrest Rate" + "\t" + "Monthly Payment" + "\t" + "Total Payment");
\t \t for(double i = 5.000; i <= 8.000; i += 0.125){
\t \t \t double monthlyIntrest = i/1200;
\t \t \t double monthlyPay = loanAmount * monthlyIntrest/(1 - 1/Math.pow(1 + monthlyIntrest, years * 12));
\t \t \t double totalPay = monthlyPay * years * 12;
\t \t \t System.out.println(i + "%" + "\t" + monthlyPay + "\t" + totalPay);
\t \t }
\t }
}
要簡單地回答你的問題,它可以像'System.out.printf(「%2F \ n」,12345.456)來來達到的; '但是你真的不應該爲了錢而使用'雙'。看看BigDecimal –
將你的double轉換爲BigDecimal並使用setScale方法。 –