最后两语句的(int),加了结果有两位小数,不加有很多小数;但是int不是整数型的吗

package programList;
import java.util.Scanner;
public class money {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

    System.out.print("Enter annual interest rate, eg:7.25% : ");    //输入年利率
    double annualInterestRate = input.nextDouble();

    double monthlyInterestRate = annualInterestRate / 1200 ;

    System.out.print("Enter number of years as an integer, eg:5 : ");   //输入整年数
    int numberOfYears = input.nextInt();

    System.out.print("Enter loan amount,eg:120000.95 : ");  //输入贷款金额
    double loanAmount = input.nextDouble();

    double monthlyPayment = loanAmount * monthlyInterestRate / (1-1 / Math.pow(1+monthlyInterestRate, numberOfYears * 12));
    double totalPayment = monthlyPayment * numberOfYears * 12 ;

    //每月支付金额
    System.out.println("The monthly payment is $" + (int)(monthlyPayment * 100) / 100.0);
    //总付款金额
    System.out.println("The total payment is $" + (int)(totalPayment * 100) / 100.0);
}

}

int型的无论乘除加减float型的(double,long)都会隐式转换为对应的类型
众所周知, 1 是整形,但是1.00是float类型的,所以整形除以一个float,就会发生隐式转化成float类型

int 转换了分子 没转换整体 你可以把分子分母扩在一起

大哥,你的int强转是转的前面的(monthlyPayment * 100)。。int除了个Double,当然还是Double喽

(int)((totalPayment * 100) / 100.0) 这样就好了

但是你的totalPayment 是double类型啊。可以用DecimalFormat df = new DecimalFormat("0.00");转换double保留需要的小数位。