总是圆了一双

编程入门 行业动态 更新时间:2024-10-20 00:23:40
本文介绍了总是圆了一双的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我怎么能总是将一个双向舍入到一个int,而不是将它向下舍入。 我知道 Math.round(double),但我希望它总是向上舍入。 因此,如果它是 3.2 ,它会四舍五入为4.

How could I always round UP a double to an int, and never round it down. I know of Math.round(double), but I want it to always round up. So if it was 3.2, it gets rounded to 4.

这是否可能?

推荐答案

您可以使用 Math.ceil()方法。

参见JavaDoc链接: docs.oracle/javase/10/docs/api/java/lang/Math.html#ceil(double)

See JavaDoc link: docs.oracle/javase/10/docs/api/java/lang/Math.html#ceil(double)

来自文档:

ceil

public static double ceil(double a)

返回大于或等于参数且等于数学整数的最小(最接近负无穷大)double值。特殊情况:

Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. Special cases:

  • 如果参数值已经等于数学整数,则结果与参数相同。
  • 如果参数为NaN或无穷大或正零或负零,则结果与参数相同。
  • 如果参数值小于大于-1但大于-1.0,则结果为负零。

请注意,Math.ceil(x)的值为确切地说-Math.floor(-x)的值。

Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x).

参数:

  • a - 一个值。

返回:

最大(最接近负无穷大)浮点值,该值大于或等于参数且等于数学整数。

The smallest (closest to negative infinity) floating-point value that is greater than or equal to the argument and is equal to a mathematical integer.

更多推荐

总是圆了一双

本文发布于:2023-10-14 22:11:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:圆了

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!