如何检查值是否为Integer类型?

时间:2021-12-04 16:06:01

I need to check if a value is an integer. I found this: How to check whether input value is integer or float?, but if I'm not mistaken, the variable there is still of type double even though the value itself is indeed an integer.

我需要检查一个值是否是一个整数。我发现这个:如何检查输入值是整数还是浮点数?但是如果我没有弄错,那么变量仍然是double类型,即使值本身确实是一个整数。

13 个解决方案

#1


65  

If input value can be in numeric form other than integer , check by

如果输入值可以是整数以外的数字形式,请检查

if (x == (int)x)
{
   // Number is integer
}

If string value is being passed , use Integer.parseInt(string_var). Please ensure error handling using try catch in case conversion fails.

如果正在传递字符串值,请使用Integer.parseInt(string_var)。如果转换失败,请确保使用try catch进行错误处理。

#2


12  

If you have a double/float/floating point number and want to see if it's an integer.

如果你有一个双/浮点/浮点数,想看看它是否是一个整数。

public boolean isDoubleInt(double d)
{
    //select a "tolerance range" for being an integer
    double TOLERANCE = 1E-5;
    //do not use (int)d, due to weird floating point conversions!
    return Math.abs(Math.floor(d) - d) < TOLERANCE;
}

If you have a string and want to see if it's an integer. Preferably, don't throw out the Integer.valueOf() result:

如果你有一个字符串,并想看看它是否是一个整数。最好不要抛弃Integer.valueOf()结果:

public boolean isStringInt(String s)
{
    try
    {
        Integer.parseInt(s);
        return true;
    } catch (NumberFormatException ex)
    {
        return false;
    }
}

If you want to see if something is an Integer object (and hence wraps an int):

如果你想看看是否有东西是一个Integer对象(因此包装了一个int):

public boolean isObjectInteger(Object o)
{
    return o instanceof Integer;
}

#3


8  

Try maybe this way

也许这样尝试

try{
    double d= Double.valueOf(someString);
    if (d==(int)d){
        System.out.println("integer"+(int)d);
    }else{
        System.out.println("double"+d);
    }
}catch(Exception e){
    System.out.println("not number");
}

But all numbers outside Integers range (like "-1231231231231231238") will be treated as doubles. If you want to get rid of that problem you can try it this way

但是整数范围之外的所有数字(如“-1231231231231231238”)将被视为双打。如果你想摆脱这个问题,你可以这样试试

try {
    double d = Double.valueOf(someString);
    if (someString.matches("\\-?\\d+")){//optional minus and at least one digit
        System.out.println("integer" + d);
    } else {
        System.out.println("double" + d);
    }
} catch (Exception e) {
    System.out.println("not number");
}

#4


6  

You should use the instanceof operator to determine if your value is Integer or not;

您应该使用instanceof运算符来确定您的值是否为Integer;

Object object = your_value;

Object object = your_value;

if(object instanceof Integer) {

Integer integer = (Integer) object ;

} else {
 //your value isn't integer
}

#5


2  

Here is the function for to check is String is Integer or not ?

这是检查字符串是否为整数的函数?

public static boolean isStringInteger(String number ){
    try{
        Integer.parseInt(number);
    }catch(Exception e ){
        return false;
    }
    return true;
}

#6


1  

To check if a String contains digit character which represent an integer, you can use Integer.parseInt().

要检查String是否包含表示整数的数字字符,可以使用Integer.parseInt()。

To check if a double contains a value which can be an integer, you can use Math.floor() or Math.ceil().

要检查double是否包含可以是整数的值,可以使用Math.floor()或Math.ceil()。

#7


1  

this is the shortest way I know with negative integers enabled:

这是我知道启用负整数的最短路径:

Object myObject = "-1";

if(Pattern.matches("\\-?\\d+", (CharSequence) myObject);)==true)
{
    System.out.println("It's an integer!");
}

And this is the way with negative integers disabled:

这是禁用负整数的方法:

Object myObject = "1";

if(Pattern.matches("\\d+", (CharSequence) myObject);)==true)
{
    System.out.println("It's an integer!");
}

#8


1  

You need to first check if it's a number. If so you can use the Math.Round method. If the result and the original value are equal then it's an integer.

你需要先检查它是否是一个数字。如果是这样,您可以使用Math.Round方法。如果结果和原始值相等则则为整数。

#9


1  

This can work:

这可以工作:

int no=0;
try
{
    no=Integer.parseInt(string);
    if(string.contains("."))
    {
        if(string.contains("f"))
        {
            System.out.println("float");
        }
        else
            System.out.println("double");
    }
}

catch(Exception ex)
{
    Console.WriteLine("not numeric or string");
}

#10


0  

Try this snippet of code

试试这段代码吧

private static boolean isStringInt(String s){
    Scanner in=new Scanner(s);
    return in.hasNextInt();
}

#11


0  

Perhaps this little class helps. I firstly wanted to jump to the next line every second loop, trying to do so by checking if the float used to loop with, was an integer if divided by 2. In the end, it gave me this code:

也许这个小课有帮助。我首先想要每隔一个循环跳到下一行,尝试通过检查浮动用于循环,是否为整数除以2.最后,它给了我这个代码:

public class Test {

    private static int sequencesPerLine = 20;
    private static int loops = 6400;

    private static long number = 999999999999999999L;


    public static void main(String[] args) {

        for (float i=0; i<loops; i++) {
        int integer = (int) i / sequencesPerLine;

        if (i / sequencesPerLine == integer) {
            System.out.print("\n");
        }
        System.out.print(number);
        }
    }
}

sequencesPerLine is how many times to print number before jumping to the next line. Changing sequencesPerLine to 2, will give the result I was originally looking for.

sequencesPerLine是跳转到下一行之前打印数量的次数。将sequencesPerLine更改为2将给出我最初寻找的结果。

#12


0  

You can use modulus %, the solution is so simple:

你可以使用模数%,解决方案是如此简单:

import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter first number");
    Double m = scan.nextDouble();
    System.out.println("Enter second number");
    Double n= scan.nextDouble();

    if(m%n==0) 
    {
        System.out.println("Integer");
    }
    else
    {
        System.out.println("Double");
    }


}
}

#13


-1  

I always believe in providing algorithm so that others can benifit from it also.

我总是相信提供算法,以便其他人也能从中受益。

variable v;
if(sizeof(v)==4)
    //integer
else
    //not-integer

#1


65  

If input value can be in numeric form other than integer , check by

如果输入值可以是整数以外的数字形式,请检查

if (x == (int)x)
{
   // Number is integer
}

If string value is being passed , use Integer.parseInt(string_var). Please ensure error handling using try catch in case conversion fails.

如果正在传递字符串值,请使用Integer.parseInt(string_var)。如果转换失败,请确保使用try catch进行错误处理。

#2


12  

If you have a double/float/floating point number and want to see if it's an integer.

如果你有一个双/浮点/浮点数,想看看它是否是一个整数。

public boolean isDoubleInt(double d)
{
    //select a "tolerance range" for being an integer
    double TOLERANCE = 1E-5;
    //do not use (int)d, due to weird floating point conversions!
    return Math.abs(Math.floor(d) - d) < TOLERANCE;
}

If you have a string and want to see if it's an integer. Preferably, don't throw out the Integer.valueOf() result:

如果你有一个字符串,并想看看它是否是一个整数。最好不要抛弃Integer.valueOf()结果:

public boolean isStringInt(String s)
{
    try
    {
        Integer.parseInt(s);
        return true;
    } catch (NumberFormatException ex)
    {
        return false;
    }
}

If you want to see if something is an Integer object (and hence wraps an int):

如果你想看看是否有东西是一个Integer对象(因此包装了一个int):

public boolean isObjectInteger(Object o)
{
    return o instanceof Integer;
}

#3


8  

Try maybe this way

也许这样尝试

try{
    double d= Double.valueOf(someString);
    if (d==(int)d){
        System.out.println("integer"+(int)d);
    }else{
        System.out.println("double"+d);
    }
}catch(Exception e){
    System.out.println("not number");
}

But all numbers outside Integers range (like "-1231231231231231238") will be treated as doubles. If you want to get rid of that problem you can try it this way

但是整数范围之外的所有数字(如“-1231231231231231238”)将被视为双打。如果你想摆脱这个问题,你可以这样试试

try {
    double d = Double.valueOf(someString);
    if (someString.matches("\\-?\\d+")){//optional minus and at least one digit
        System.out.println("integer" + d);
    } else {
        System.out.println("double" + d);
    }
} catch (Exception e) {
    System.out.println("not number");
}

#4


6  

You should use the instanceof operator to determine if your value is Integer or not;

您应该使用instanceof运算符来确定您的值是否为Integer;

Object object = your_value;

Object object = your_value;

if(object instanceof Integer) {

Integer integer = (Integer) object ;

} else {
 //your value isn't integer
}

#5


2  

Here is the function for to check is String is Integer or not ?

这是检查字符串是否为整数的函数?

public static boolean isStringInteger(String number ){
    try{
        Integer.parseInt(number);
    }catch(Exception e ){
        return false;
    }
    return true;
}

#6


1  

To check if a String contains digit character which represent an integer, you can use Integer.parseInt().

要检查String是否包含表示整数的数字字符,可以使用Integer.parseInt()。

To check if a double contains a value which can be an integer, you can use Math.floor() or Math.ceil().

要检查double是否包含可以是整数的值,可以使用Math.floor()或Math.ceil()。

#7


1  

this is the shortest way I know with negative integers enabled:

这是我知道启用负整数的最短路径:

Object myObject = "-1";

if(Pattern.matches("\\-?\\d+", (CharSequence) myObject);)==true)
{
    System.out.println("It's an integer!");
}

And this is the way with negative integers disabled:

这是禁用负整数的方法:

Object myObject = "1";

if(Pattern.matches("\\d+", (CharSequence) myObject);)==true)
{
    System.out.println("It's an integer!");
}

#8


1  

You need to first check if it's a number. If so you can use the Math.Round method. If the result and the original value are equal then it's an integer.

你需要先检查它是否是一个数字。如果是这样,您可以使用Math.Round方法。如果结果和原始值相等则则为整数。

#9


1  

This can work:

这可以工作:

int no=0;
try
{
    no=Integer.parseInt(string);
    if(string.contains("."))
    {
        if(string.contains("f"))
        {
            System.out.println("float");
        }
        else
            System.out.println("double");
    }
}

catch(Exception ex)
{
    Console.WriteLine("not numeric or string");
}

#10


0  

Try this snippet of code

试试这段代码吧

private static boolean isStringInt(String s){
    Scanner in=new Scanner(s);
    return in.hasNextInt();
}

#11


0  

Perhaps this little class helps. I firstly wanted to jump to the next line every second loop, trying to do so by checking if the float used to loop with, was an integer if divided by 2. In the end, it gave me this code:

也许这个小课有帮助。我首先想要每隔一个循环跳到下一行,尝试通过检查浮动用于循环,是否为整数除以2.最后,它给了我这个代码:

public class Test {

    private static int sequencesPerLine = 20;
    private static int loops = 6400;

    private static long number = 999999999999999999L;


    public static void main(String[] args) {

        for (float i=0; i<loops; i++) {
        int integer = (int) i / sequencesPerLine;

        if (i / sequencesPerLine == integer) {
            System.out.print("\n");
        }
        System.out.print(number);
        }
    }
}

sequencesPerLine is how many times to print number before jumping to the next line. Changing sequencesPerLine to 2, will give the result I was originally looking for.

sequencesPerLine是跳转到下一行之前打印数量的次数。将sequencesPerLine更改为2将给出我最初寻找的结果。

#12


0  

You can use modulus %, the solution is so simple:

你可以使用模数%,解决方案是如此简单:

import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter first number");
    Double m = scan.nextDouble();
    System.out.println("Enter second number");
    Double n= scan.nextDouble();

    if(m%n==0) 
    {
        System.out.println("Integer");
    }
    else
    {
        System.out.println("Double");
    }


}
}

#13


-1  

I always believe in providing algorithm so that others can benifit from it also.

我总是相信提供算法,以便其他人也能从中受益。

variable v;
if(sizeof(v)==4)
    //integer
else
    //not-integer