Java中检查数字是否为整数

发布于 2024-10-28 20:53:24 字数 115 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

谁人与我共长歌 2024-11-04 20:53:24

快速而肮脏...

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

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

Quick and dirty...

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

edit: This is assuming x is already in some other numeric form. If you're dealing with strings, look into Integer.parseInt.

淑女气质 2024-11-04 20:53:24

再举一个例子:)

double a = 1.00

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

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

One example more :)

double a = 1.00

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

In this example, ceil can be used and have the exact same effect.

会发光的星星闪亮亮i 2024-11-04 20:53:24
/**
 * 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.
}
/**
 * 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.
}
紙鸢 2024-11-04 20:53:24

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

据我所知,执行此操作的最佳方法是确定某个 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),这可能会中断,并且您应该意识到,不可能检查高于该浮点数的完整性价值。

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:

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

then

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

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.

橙味迷妹 2024-11-04 20:53:24

对于 ZI ,假设您指的是 Integers ,即 3,-5,77 而不是 3.14, 4.02 等。

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

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

With Z I assume you mean Integers , i.e 3,-5,77 not 3.14, 4.02 etc.

A regular expression may help:

Pattern isInteger = Pattern.compile("\\d+");
音栖息无 2024-11-04 20:53:24
    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”就是一个整数。

    double x == 2.15;

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

I think you can similarly use the Math.ceil() method to verify whether x is an integer or not. This works because Math.ceil or Math.floor rounds up x to the nearest integer (say y) and if x==y then our original `x' was an integer.

浸婚纱 2024-11-04 20:53:24
    if((number%1)!=0)
    {
        System.out.println("not a integer");
    }
    else
    {
        System.out.println("integer");
    }
    if((number%1)!=0)
    {
        System.out.println("not a integer");
    }
    else
    {
        System.out.println("integer");
    }
万人眼中万个我 2024-11-04 20:53:24

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


   }

change x to 1 and output is integer, else its not an integer add to count example whole numbers, decimal numbers etc.

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


   }
以酷 2024-11-04 20:53:24

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

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

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

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

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:

public boolean isNotInteger(double x) {
    return x - Math.floor(x) > 0;
}
诗笺 2024-11-04 20:53:24
 int x = 3;

 if(ceil(x) == x) {

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

 } else {

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

 }
 int x = 3;

 if(ceil(x) == x) {

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

 } else {

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

 }
空袭的梦i 2024-11-04 20:53:24

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

static boolean isInteger(int n) 
{ 
return (int)(Math.ceil(n)) == (int)(Math.floor(n)); 
} 

Check if ceil function and floor function returns the same value

static boolean isInteger(int n) 
{ 
return (int)(Math.ceil(n)) == (int)(Math.floor(n)); 
} 
暮色兮凉城 2024-11-04 20:53:24

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

You can just use x % 1 == 0 because x % 1 gives the residual value of x / 1

三生路 2024-11-04 20:53:24

// 在 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;
}

// in C language.. but the algo is same

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文