Java中如何判断一个数字是否为整数

46

有没有一种方法或快速方式可以在Java中检查一个数字是否为整数(属于Z域)?

我考虑过可能将其从四舍五入的数字中减去,但我没有找到任何可以帮助我的方法。

我应该在哪里检查? Integer Api?


1
你是从字符串中解析数字还是已经有了一些浮点数或双精度数? - Serhiy
4
你的意思是“属于Z领域”是什么意思? - notthetup
Z字段 - 这意味着它是整数。 - Unknown user
7
@ntt: N表示自然数,Z表示整数,Q表示有理数,R表示实数,C表示复数。 - MicSim
明白了!谢谢!那么你把它保存为浮点数或其他类型,想要检查它是否是“整数”? - notthetup
显示剩余3条评论
13个回答

78

快速而简单...

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

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


1
通常我有一个函数isInteger(),它在Integer.parseInt上有try catch,如果发生异常则返回false。 - Serhiy
1
这是一个很好的答案,我因此点赞了它 -- 但是,请确保您理解浮点数表示的限制! - Alexander Questioning Bresee
刚开始这个解决方案对我来说不起作用,但最终我还是使用了它: if (x.intValue() == x.doubleValue()) {} - taha

17

再举一个例子 :)

double a = 1.00

if(floor(a) == a) {
   // a is an integer
} else {
   //a is not an integer.
}
在这个例子中,可以使用 ceil 函数并且具有完全相同的效果。

10
/**
 * 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.
}

4

如果你在讨论浮点数值,由于其格式的特性,你必须非常小心。

我知道的最好方法是决定一些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),则此方法可能会失效,您应该知道,在那个值以上的浮点数上不可能检查整数部分。


1
+1:如果您使用大于2^24(约1600万)的整数,则会在“float”上获得舍入误差。如果您使用“double”,则会在大于2^53(8万亿)的整数上获得舍入误差。 - Peter Lawrey

1

通过 Z,我假设你指的是整数,即3、-5、77,而不是3.14、4.02等。

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

Pattern isInteger = Pattern.compile("\\d+");

他所指的Z是整数,http://en.wikipedia.org/wiki/Integer <-- Z是用来表示该集合的正常符号。 - Alexander Questioning Bresee
@notthetup - 我不这么认为。 - GKFX

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.floorx四舍五入到最近的整数(比如y),如果x==y,那么我们原来的`x'就是一个整数。


0

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

我假设应该被测试的输入是double类型,而不是字符串。

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

public boolean isNotInteger(double x) {
    return x - Math.floor(x) > 0;
}

0
    if((number%1)!=0)
    {
        System.out.println("not a integer");
    }
    else
    {
        System.out.println("integer");
    }

在Java中要小心浮点数和双精度数,它们(缺乏的)精度可能会引起问题。请使用BigDecimal。 - Pablo Lozano
@PabloLozano,我不明白你的问题,请举个例子解释一下你的评论。 - Dhananjay M

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);


   }

-1
 int x = 3;

 if(ceil(x) == x) {

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

 } else {

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

 }

1
这基本上重复了顶部的答案,而且如果不包括 ceil(int) 函数,这是无用的。另外,如果 x 已经是一个 int,那么这个检查有什么意义呢? - randers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接