Java中检查数字是否为整数
Java中是否有任何方法或快速方法来检查数字是否为整数(属于Z字段)?
我想也许从四舍五入的数字中减去它,但我没有找到任何方法可以帮助我解决这个问题。
我应该去哪里检查?整数 API?
Is there any method or quick way to check whether a number is an Integer (belongs to Z field) in Java?
I thought of maybe subtracting it from the rounded number, but I didn't find any method that will help me with this.
Where should I check? Integer Api?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
快速而肮脏...
编辑:这是假设 x 已经采用其他数字形式。如果您正在处理字符串,请查看
Integer.parseInt
。Quick and dirty...
edit: This is assuming x is already in some other numeric form. If you're dealing with strings, look into
Integer.parseInt
.再举一个例子:)
在这个例子中,可以使用 ceil 并具有完全相同的效果。
One example more :)
In this example, ceil can be used and have the exact same effect.
如果您谈论浮点值,由于格式的性质,您必须非常小心。
据我所知,执行此操作的最佳方法是确定某个 epsilon 值,例如 0.000001f,然后执行以下操作:
然后,
本质上您要检查 z 和 z 的整数情况在某些范围内是否具有相同的大小宽容。这是必要的,因为浮动本质上是不精确的。
但是请注意:如果您的浮点数的大小大于
Integer.MAX_VALUE
(2147483647),这可能会中断,并且您应该意识到,不可能检查高于该浮点数的完整性价值。if you're talking floating point values, you have to be very careful due to the nature of the format.
the best way that i know of doing this is deciding on some epsilon value, say, 0.000001f, and then doing something like this:
then
essentially you're checking to see if z and the integer case of z have the same magnitude within some tolerance. This is necessary because floating are inherently imprecise.
NOTE, HOWEVER: this will probably break if your floats have magnitude greater than
Integer.MAX_VALUE
(2147483647), and you should be aware that it is by necessity impossible to check for integral-ness on floats above that value.对于 ZI ,假设您指的是 Integers ,即 3,-5,77 而不是 3.14, 4.02 等。
正则表达式可能会有所帮助:
With Z I assume you mean Integers , i.e 3,-5,77 not 3.14, 4.02 etc.
A regular expression may help:
我认为您可以类似地使用 Math.ceil() 方法来验证 x 是否是整数。这是有效的,因为
Math.ceil
或Math.floor
将x
向上舍入到最接近的整数(例如y
),并且如果x==y
那么我们原来的“x”就是一个整数。I think you can similarly use the
Math.ceil()
method to verify whetherx
is an integer or not. This works becauseMath.ceil
orMath.floor
rounds upx
to the nearest integer (sayy
) and ifx==y
then our original `x' was an integer.将 x 更改为 1 并且输出为整数,否则它不是整数添加到计数示例整数、小数等。
change x to 1 and output is integer, else its not an integer add to count example whole numbers, decimal numbers etc.
所有给出的解决方案都很好,但是大多数解决方案都会给静态代码分析(例如 SONAR)带来问题:“不应测试浮点数是否相等”(请参阅https://jira.sonarsource.com/browse/RSPEC-1244)。
我假设应该测试的输入是双精度的,而不是字符串。
作为解决方法,我测试数字是否为整数:
All given solutions are good, however most of them can give issues with Static Code Analysis (e.g. SONAR): "Floating point numbers should not be tested for equality" (see https://jira.sonarsource.com/browse/RSPEC-1244).
I am assuming that input that should be tested is double, not string.
As a workaround, I test numbers for not being integers:
检查 ceil 函数和 Floor 函数返回的值是否相同
Check if ceil function and floor function returns the same value
您可以只使用
x % 1 == 0
因为 x % 1 给出 x / 1 的残差值You can just use
x % 1 == 0
because x % 1 gives the residual value of x / 1// 在 C 语言中..但算法是相同的
// in C language.. but the algo is same