java 判断对象类型是否为数字类型_java-如何检查值是否为整数类型?

java-如何检查值是否为整数类型?

我需要检查值是否为整数。 我发现了这一点:如何检查输入值是整数还是浮点数?但是,如果我没有记错的话,即使该值本身确实是integer,该变量的类型仍然为double。

Voldemort asked 2020-02-20T06:14:50Z

13个解决方案

80 votes

如果输入值可以是整数以外的其他数字形式,请按

if (x == (int)x)

{

// Number is integer

}

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

Mudassir Hasan answered 2020-02-20T06:15:03Z

16 votes

如果您有一个double / float / floating point数字,并想知道它是否为整数。

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;

}

如果您有一个字符串,并且想查看它是否为整数。 最好不要抛出int结果:

public boolean isStringInt(String s)

{

try

{

Integer.parseInt(s);

return true;

} catch (NumberFormatException ex)

{

return false;

}

}

如果要查看某物是否是Integer对象(因此包装了int):

public boolean isObjectInteger(Object o)

{

return o instanceof Integer;

}

Ryan Amos answered 2020-02-20T06:15:32Z

8 votes

尝试这样

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

}

但是所有超出整数范围的数字(例如“ -123123123123123121238”)都将被视为双精度。 如果您想摆脱这个问题,可以这样尝试

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

}

Pshemo answered 2020-02-20T06:15:56Z

8 votes

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

对象object = your_value;

if(object instanceof Integer) {

Integer integer = (Integer) object ;

} else {

//your value isn't integer

}

Andrei Amarfii answered 2020-02-20T06:16:21Z

4 votes

if (x % 1 == 0)

// x is an integer

这里x是一个数字原语:short,int,long,float或double

Gayan Weerakutti answered 2020-02-20T06:16:41Z

3 votes

这是检查String是否为Integer的函数?

public static boolean isStringInteger(String number ){

try{

Integer.parseInt(number);

}catch(Exception e ){

return false;

}

return true;

}

Yasir Shabbir Choudhary answered 2020-02-20T06:17:01Z

1 votes

要检查字符串是否包含代表整数的数字字符,可以使用Math.floor()。

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

Code-Apprentice answered 2020-02-20T06:17:29Z

1 votes

这是我知道启用负整数的最短方法:

Object myObject = "-1";

if(Pattern.matches("\\-?\\d+", (CharSequence) myObject);)==true)

{

System.out.println("It's an integer!");

}

这是禁用负整数的方法:

Object myObject = "1";

if(Pattern.matches("\\d+", (CharSequence) myObject);)==true)

{

System.out.println("It's an integer!");

}

august0490 answered 2020-02-20T06:17:54Z

1 votes

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

Amjad Abdelrahman answered 2020-02-20T06:18:14Z

1 votes

这可以工作:

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

}

Ravindra Bagale answered 2020-02-20T06:18:33Z

1 votes

也许这一堂课会有所帮助。 首先,我想每隔第二个循环跳到下一行,尝试通过检查用于循环的浮点数是否除以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

int integer = (int) i / sequencesPerLine;

if (i / sequencesPerLine == integer) {

System.out.print("\n");

}

System.out.print(number);

}

}

}

sequencesPerLine是跳到下一行之前要打印number的次数。将sequencesPerLine更改为2,将得到我原来想要的结果。

Andreas -he-her- answered 2020-02-20T06:18:58Z

0 votes

尝试这段代码

private static boolean isStringInt(String s){

Scanner in=new Scanner(s);

return in.hasNextInt();

}

Daniel Chang answered 2020-02-20T06:19:19Z

0 votes

您可以使用模数%,解决方案非常简单:

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

}

}

}

Ali.Ghodrat answered 2020-02-20T06:19:39Z

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值