gpt4 book ai didi

java - 如何让我的 Java 程序四舍五入?

转载 作者:行者123 更新时间:2023-12-01 11:05:39 27 4
gpt4 key购买 nike

我希望我的 Java 程序将数字四舍五入到小数点后两位。我一直在查看我的类 Material ,但找不到解决方案。这是代码

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

我更多的是寻找教程或只是一些关于如何自己做的建议。

最佳答案

如果你只需要四舍五入输出,你可以这样做

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

关于java - 如何让我的 Java 程序四舍五入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32978497/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com