java math rint_Java StrictMath rint()用法及代码示例

rint()是Java中StrictMath类的内置方法,用于获取值最接近参数且等于整数的double值。它返回一个整数值,当作为整数的两个double值均相等地接近给定参数的值时,它甚至是整数。当参数值已经等于整数时,它返回与参数相同的值;当参数为NaN或无穷大或正零或负零时,它返回与参数相同的值。

用法:

public static double rint(double num)

参数:该方法接受要使用此方法转换的双精度类型的单个参数num。

返回值:该方法返回等于整数的最接近的浮点值。

例子:

Input: num =72.2

Output: 72.0

以下示例程序旨在说明rint()方法:

示例1:

// Java praogram to illustrate the

// java.lang.StrictMath.rint()

import java.lang.*;

public class Geeks {

public static void main(String[] args)

{

// Get a double number

double num1 = 87.1;

// Convert the double number using rint() method

double rint_Value = StrictMath.rint(num1);

// Print the result

System.out.println(" The Integer value closest to "

+ num1 + " = " + rint_Value);

// Get a double number

double num2 = 65.9;

// Convert the double number using rint() method

rint_Value = StrictMath.rint(num1);

// Print the result

System.out.println(" The Integer value closest to "

+ num1 + " = " + rint_Value);

}

}

输出:

The Integer value closest to 87.1 = 87.0

The Integer value closest to 87.1 = 87.0

示例2:

// Java praogram to illustrate the

// java.lang.StrictMath.rint()

import java.lang.*;

public class Geeks {

public static void main(String[] args)

{

// Get a double number

double num1 = -65.5;

// Convert the double number using rint() method

double rint_Value = StrictMath.rint(num1);

// Print the result

System.out.println(" The Integer value closest to "

+ num1 + " = " + rint_Value);

// Get a double number

double num2 = -42.7;

// Convert the double number using rint() method

rint_Value = StrictMath.rint(num1);

// Print the result

System.out.println(" The Integer value closest to "

+ num1 + " = " + rint_Value);

}

}

输出:

The Integer value closest to -65.5 = -66.0

The Integer value closest to -65.5 = -66.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值