gpt4 book ai didi

java - java中如何四舍五入到小数点后两位

转载 作者:行者123 更新时间:2023-12-01 11:57:22 25 4
gpt4 key购买 nike

最终双倍 YearDep= 0.15;//这是汽车每年损失之前的值(value)

     double HybridDepreciation= HybridCarCost * Math.pow(1-YearDep,5);
double HybridFuelCost=(Miles*5*GasCost)/HybridCarMPG;

double GasPowerDepreciation=GasPowerCost * Math.pow(1-YearDep,5);
double GasPowerFuelCost=(Miles *5*GasCost)/GasPowerMPG;

double HybridTotalCost=HybridCarCost - HybridDepreciation + HybridFuelCost;
double GasPowerTotalCost= GasPowerCost - GasPowerDepreciation + GasPowerFuelCost;

System.out.printf("The total cost for the " + HybridName + " " + "is" +" $"+ HybridTotalCost);

System.out.println();
System.out.printf("The total cost for the " + GasPowerName + " "+ "is" + " $"+ GasPowerTotalCost);

最佳答案

使用DecimalFormat为此:

DecimalFormat df = new DecimalFormat(".00");
System.out.printf("The total cost for the " + HybridName + " " + "is" +" $"+ df.format(HybridTotalCost));
System.out.println();
System.out.printf("The total cost for the " + GasPowerName + " "+ "is" + " $"+ df.format(GasPowerTotalCost));

或者你可以使用

String.format("The total cost for the  %s is $%.2f", HybridName ,HybridTotalCost)

关于java - java中如何四舍五入到小数点后两位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28337557/

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