当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Math.rint()用法及代码示例


java.lang.Math.rint() 用于将参数四舍五入到最接近的数学整数。

用法

public static double rint(double x)

参数

x= a double value

返回

It returns closest floating-point value to x that is equal to a mathematical integer.
  • 如果参数是正数或负数,此方法将返回最接近的值。
  • 如果作为数学整数的两个双精度值相等,则此方法将返回偶数的整数值。
  • 如果参数是 NaN 或无穷大或正零或负零,则此方法将返回参数值作为结果。

例子1

public class RintExample1
{
    public static void main(String[] args) 
    {
        double x = 81.68;
        // Input positive value, Output round the x
        System.out.println(Math.rint(x));
    }
}

输出:

82.0

例子2

public class RintExample2
{
    public static void main(String[] args) 
    {
        double x = -37.25;
        // Input negative zero, Output round the x 	
        System.out.println(Math.rint(x));
    }
}

输出:

-37.0

例子3

public class RintExample3
{
    public static void main(String[] args) 
    {
        double x = 80.5;
        double y = 79.5;
        // rounds upto 80 which is the nearest even double value	
        System.out.println(Math.rint(x));
        System.out.println(Math.rint(y));
    }
}

输出:

80.0
80.0

示例 4

public class RintExample4
{
    public static void main(String[] args) 
    {
        double x = 1.0/0;
        // Input positive infinity, Output positive infinity 	
        System.out.println(Math.rint(x));
    }
}

输出:

Infinity






相关用法


注:本文由纯净天空筛选整理自 Java Math.rint() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。