gpt4 book ai didi

java - Math.ceil 和 Math.rint 方法返回相同的值

转载 作者:行者123 更新时间:2023-11-29 07:37:14 25 4
gpt4 key购买 nike

Math.rint()Math.ceil() 方法返回相同的值,谁能解释一下为什么?
这些方法有什么区别?何时使用 Math.rint() 以及在何处使用 Math.ceil()

public class BasicMathDemo {

public static void main(String[] args) {

double b = 43.74;
System.out.println("The rint of "+ b +" is : "+Math.rint(b));
System.out.println("The ceiling of " + b +" is: "+ Math.ceil(b));

}
}

输出是:

The rint of 43.74 is : 44.0
The ceiling of 43.74 is: 44.0

最佳答案

Math.rint() 将您的数字四舍五入为最接近的整数。 See

Math.ceil() 将您的数字四舍五入到最接近的较高整数。 See

Math.floor() 将您的数字向下舍入到最接近的小整数。 See

例子:

float number = 3.38;
double rint = Math.rint(number); // This will return 3
double ceil = Math.ceil(number); // This will return 4
double floor = Math.floor(number); // This will return 3

关于java - Math.ceil 和 Math.rint 方法返回相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34171497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com