为了账号安全,请及时绑定邮箱和手机立即绑定

如何实现 Math.round 以实现 3dp 输出?

如何实现 Math.round 以实现 3dp 输出?

杨__羊羊 2021-08-19 17:39:09
public class Favorite {    public static void main(String[] arguments) {    String itemName = "Golden Beans";    double offerPrice = 314;    int sellPrice = 321;    double value = (sellPrice - offerPrice);    int cashStack = 500_000;    double percentageProfit = ((value / offerPrice) * 100);    System.out.println("Approx. Offer Price is " + offerPrice);    System.out.println("Approx. Sell Price is " + sellPrice);    System.out.println("The potential profit margin is " + value);    System.out.println("With a cash stack of " + cashStack + " we can buy " + cashStack / offerPrice + " " + itemName +"s");    System.out.println("The profit margin of " + itemName + " as a percentage is " + percentageProfit);    }}制作一个我决定买卖价格的小程序,它告诉我利润率。完毕然后找出我可以用 500,000 购买多少件商品。完毕然后通过让程序告诉我购买价格的利润来使其更高级。完毕然后让程序输出到小数点后三位。(这就是我被卡住的地方!)
查看完整描述

3 回答

?
狐的传说

TA贡献1804条经验 获得超3个赞

首先阅读文档 - java docs:

System.out.printf("%.3f", res);

或十进制格式

System.out.println(new java.text.DecimalFormat("#.000").format(500.12321313));


查看完整回答
反对 回复 2021-08-19
?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

尝试printf()而不是println()使用格式%.3f

System.out.printf("The profit margin of %s as a percentage is %.3f%n" itemName,percentageProfit);



查看完整回答
反对 回复 2021-08-19
  • 3 回答
  • 0 关注
  • 115 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信