【问题标题】:Java rounding int values depending on arithmetic result [duplicate]Java根据算术结果舍入int值[重复]
【发布时间】:2016-11-02 22:07:45
【问题描述】:

假设我对int 值进行了一些算术计算(例如除法然后加法),然后还想将结果存储在int 值中。

如何将大于或等于 x.5 的值向上舍入,然后在小于 x.5 时向下舍入?

所以11.5 = 1211.3 = 11

【问题讨论】:

  • 致电Math.round()?

标签: java math floating-point rounding precision


【解决方案1】:

Java.Math 库提供地板和天花板函数。

   import Java.Math.*;

    System.out.println(Math.ceil(13.4)); // Prints 14
    System.out.println(Math.floor(13.4)); // Prints 13

同一个库中还提供了舍入函数

    System.out.println(Math.round(10.6)); // Prints 11
    System.out.println(Math.round(10.4)); // Prints 10

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 2017-11-10
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    相关资源
    最近更新 更多