将一个浮点数整数到java中的下一个整数值 [英] round a floating-point number to the next integer value in java

查看:379
本文介绍了将一个浮点数整数到java中的下一个整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能在Java中将浮点数加到下一个整数值?假设


2.1 - > 3



3.001 - > 4 p>

4.5 - > 5



7.9 - > 8



解决方案

你应该看看Java的数学包中的上限四舍五入:Math.ceil






编辑:添加Math.ceil的javadoc。这可能是值得读数学的所有方法。



http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#ceil%28double%29


public static double ceil(double a)



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


  • 如果参数值已经等于一个数学整数,那么结果和参数一样。
  • >
  • 如果参数是NaN或无穷大或正零或负零,则结果与参数相同。
  • 如果参数值较小



请注意,数学的值.ceil(x)正是
-Math.floor(-x)的值。


how can i round up a floating point number to the next integer value in Java? Suppose

2.1 -->3

3.001 -->4

4.5 -->5

7.9 -->8

解决方案

You should look at ceiling rounding up in java's math packages: Math.ceil


EDIT: Added the javadoc for Math.ceil. It may be worth reading all the method in Math.

http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#ceil%28double%29

public static double ceil(double a)

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:

  • If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
  • If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
  • If the argument value is less than zero but greater than -1.0, then the result is negative zero.

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

这篇关于将一个浮点数整数到java中的下一个整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆