strictmath_Java StrictMath round()方法与示例

strictmath

Syntax:

句法:

    public static long round(double d);
    public static int round(float f);

StrictMath类round()方法 (StrictMath Class round() method)

  • round() method is available in java.lang package.

    round()方法在java.lang包中可用。

  • round(double d) method is used to return the closest long value to the given argument.

    round(double d)方法用于将最接近的long值返回给定参数。

  • round(float f) method is used to return the nearest int value to the given argument and it is rounded to an integer by adding ½ and convert the result from float to int.

    round(float f)方法用于将最接近的int值返回给定参数,并通过加½将其舍入为整数并将结果从float转换为int。

  • These methods don't throw an exception.

    这些方法不会引发异常。

  • These are static methods, it is accessible with the class name and, if we try to access these methods with the class object then we will not get any error.

    这些是静态方法,可以通过类名进行访问,如果尝试使用类对象访问这些方法,则不会出现任何错误。

Parameter(s):

参数:

  • float/double – represents the value to be rounded.

    float / double-表示要四舍五入的值。

Return value:

返回值:

The return type of this method is int / long – it returns the rounded value based on the given argument type.

此方法的返回类型为int / long-它根据给定的参数类型返回舍入值。

Note:

注意:

  • If we pass NaN, the method returns 0.

    如果传递NaN,则该方法返回0。

  • If we pass a negative infinity, the method returns Long.MIN_VALUE.

    如果我们传递一个负无穷大,则该方法返回Long.MIN_VALUE。

  • If we pass a positive infinity, the method returns Long.MAX_VALUE.

    如果我们传递一个正无穷大,则该方法返回Long.MAX_VALUE。

  • If we pass a value which is less than or equal to Integer.MIN_VALUE/ Long.MIN_VALUE, the method returns Integer.MIN_VALUE/ Long.MIN_VALUE.

    如果传递的值小于或等于Integer.MIN_VALUE / Long.MIN_VALUE,则该方法返回Integer.MIN_VALUE / Long.MIN_VALUE。

  • If we pass a value which is greater than or Integer.MAX_VALUE/ Long.MAX_VALUE, the method returns Integer.MAX_VALUE/ Long.MAX_VALUE.

    如果传递的值大于或Integer.MAX_VALUE / Long.MAX_VALUE,则该方法返回Integer.MAX_VALUE / Long.MAX_VALUE。

Example:

例:

// Java program to demonstrate the example 
// of round() method of StrictMath class

public class Round {
    public static void main(String[] args) {
        // variable declarations
        double d1 = -1.0 / 0.0;
        double d2 = 1.0 / 0.0;
        double d3 = 1234.56;
        double d4 = 1234.42;

        float f1 = -1.0f / 0.0f;
        float f2 = 1.0f / 0.0f;
        float f3 = 1234.56f;
        float f4 = 1234.42f;

        System.out.println();
        System.out.println("round(double): ");

        // Here , we will get (Long.MIN_VALUE) and we are
        // passing parameter whose value is (-Infinity)
        System.out.println("StrictMath.round (d1): " + StrictMath.round(d1));

        // Here , we will get (Long.MAX_VALUE) and we are 
        // passing parameter whose value is (Infinity)
        System.out.println("StrictMath.round (d2): " + StrictMath.round(d2));

        // Here , we will get (1235) and we are 
        // passing parameter whose value is (1234.56)
        System.out.println("StrictMath.round (d3): " + StrictMath.round(d3));

        // Here , we will get (1234) and we are passing
        //parameter whose value is (1234.12)
        System.out.println("StrictMath.round (d4): " + StrictMath.round(d4));

        System.out.println();
        System.out.println("round(float): ");

        // Here , we will get (Integer.MIN_VALUE) and we are 
        // passing parameter whose value is (-Infinity)
        System.out.println("StrictMath. round (f1): " + StrictMath.round(f1));

        // Here , we will get (Integer.MAX_VALUE) and we are 
        // passing parameter whose value is (Infinity)
        System.out.println("StrictMath. round (f2): " + StrictMath.round(f2));

        // Here , we will get (1235) and we are
        // passing parameter whose value is (1234.56)
        System.out.println("StrictMath. round (f3): " + StrictMath.round(f3));

        // Here , we will get (1234) and we are
        // passing parameter whose value is (1234.12)
        System.out.println("StrictMath. round (f4): " + StrictMath.round(f4));
    }
}

Output

输出量

round(double): 
StrictMath.round (d1): -9223372036854775808
StrictMath.round (d2): 9223372036854775807
StrictMath.round (d3): 1235
StrictMath.round (d4): 1234

round(float): 
StrictMath. round (f1): -2147483648
StrictMath. round (f2): 2147483647
StrictMath. round (f3): 1235
StrictMath. round (f4): 1234


翻译自: https://www.includehelp.com/java/strictmath-round-method-with-example.aspx

strictmath

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值