36

是否有任何方法或快速方法来检查数字是否为 Java 中的整数(属于 Z 字段)?

我想也许可以从四舍五入的数字中减去它,但我没有找到任何可以帮助我解决这个问题的方法。

我应该在哪里检查?整数 API?

4

13 回答 13

72

又快又脏……

if (x == (int)x)
{
   ...
}

编辑:这是假设 x 已经是其他数字形式。如果您正在处理字符串,请查看Integer.parseInt.

于 2011-03-31T15:47:37.403 回答
16

再举一个例子:)

double a = 1.00

if(floor(a) == a) {
   // a is an integer
} else {
   //a is not an integer.
}

在此示例中,可以使用 ceil 并具有完全相同的效果。

于 2011-03-31T16:01:51.023 回答
5
/**
 * Check if the passed argument is an integer value.
 *
 * @param number double
 * @return true if the passed argument is an integer value.
 */
boolean isInteger(double number) {
    return number % 1 == 0;// if the modulus(remainder of the division) of the argument(number) with 1 is 0 then return true otherwise false.
}
于 2018-07-05T11:58:04.170 回答
3

如果您在谈论浮点值,由于格式的性质,您必须非常小心。

我知道这样做的最好方法是确定一些 epsilon 值,例如 0.000001f,然后执行以下操作:

boolean nearZero(float f)
{
    return ((-episilon < f) && (f <epsilon)); 
}

然后

if(nearZero(z-(int)z))
{ 
    //do stuff
}

本质上,您正在检查 z 和 z 的整数大小写是否在某个容差范围内具有相同的大小。这是必要的,因为浮动本质上是不精确的。

但是请注意:如果您的浮点数大于Integer.MAX_VALUE(2147483647),这可能会中断,并且您应该知道,必须不可能检查高于该值的浮点数的完整性。

于 2011-03-31T15:56:43.123 回答
1

使用 ZI 假设您的意思是 Integers ,即 3,-5,77 而不是 3.14, 4.02 等。

正则表达式可能会有所帮助:

Pattern isInteger = Pattern.compile("\\d+");
于 2011-03-31T15:53:17.373 回答
1
    double x == 2.15;

    if(Math.floor(x) == x){
        System.out.println("an integer");
    } else{
        System.out.println("not an integer");
    }

我认为您可以类似地使用该Math.ceil()方法来验证是否x为整数。这是有效的,因为Math.ceilMath.floor四舍五入x到最接近的整数(比如y),如果x==y我们原来的“x”是一个整数。

于 2020-05-09T02:25:53.023 回答
1

您可以使用x % 1 == 0,因为 x % 1 给出了 x / 1 的残值

于 2021-04-05T02:52:43.897 回答
0
    if((number%1)!=0)
    {
        System.out.println("not a integer");
    }
    else
    {
        System.out.println("integer");
    }
于 2014-10-29T10:31:23.697 回答
0

将 x 更改为 1 并且输出是整数,否则它不是整数添加到计数示例整数、十进制数等。

   double x = 1.1;
   int count = 0;
   if (x == (int)x)
    {
       System.out.println("X is an integer: " + x);
       count++; 
       System.out.println("This has been added to the count " + count);
    }else
   {
       System.out.println("X is not an integer: " + x);
       System.out.println("This has not been added to the count " + count);


   }
于 2013-12-08T18:19:07.353 回答
0

检查 ceil 函数和 floor 函数是否返回相同的值

static boolean isInteger(int n) 
{ 
return (int)(Math.ceil(n)) == (int)(Math.floor(n)); 
} 
于 2019-02-10T16:16:26.007 回答
0

所有给定的解决方案都很好,但是它们中的大多数可能会给静态代码分析(例如 SONAR)带来问题:“不应测试浮点数是否相等”(请参阅​​ https://jira.sonarsource.com/browse/RSPEC-1244)。

我假设应该测试的输入是双重的,而不是字符串。

作为一种解决方法,我测试数字是否不是整数:

public boolean isNotInteger(double x) {
    return x - Math.floor(x) > 0;
}
于 2021-08-18T13:34:15.757 回答
0
 int x = 3;

 if(ceil(x) == x) {

  System.out.println("x is an integer");

 } else {

  System.out.println("x is not an integer");

 }
于 2016-05-08T11:11:27.030 回答
-3

// 在 C 语言中.. 但算法是一样的

#include <stdio.h>

int main(){
  float x = 77.6;

  if(x-(int) x>0)
    printf("True! it is float.");
  else
    printf("False! not float.");        

  return 0;
}
于 2017-02-08T10:48:23.987 回答