【问题标题】:How do I round this input below to a whole number? [duplicate]如何将下面的输入四舍五入为整数? [复制]
【发布时间】:2020-12-31 12:23:47
【问题描述】:
Avg = ((int)2*avg1 + 3*focavg + 5*critrating / 10);
System.out.println("Overall movie rating: " + Avg);

我将Avg 存储为双精度数,指令要求我将此代码的输出四舍五入为整数,但它总是以小数形式出现。

代码输出:电影总评分:82.57666666666667

有没有办法可以将其四舍五入到 83?

【问题讨论】:

  • 了解 round()、floor() 和 ceil() 函数
  • 请在提问前搜索something,并在问题中显示您努力的结果。
  • 双平均 = 82.57666666666667; int round = (int) Math.round(avg);

标签: java


【解决方案1】:

您可以尝试导入“java.lang.Math”,并可以在打印语句中使用 Math 类的天花板方法: System.out.println("电影总评分:" + Math.ceil(Avg));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 2015-01-26
    • 2023-03-27
    相关资源
    最近更新 更多