如何检查值是否为Integer类型? [英] How can I check if a value is of type Integer?

查看:190
本文介绍了如何检查值是否为Integer类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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

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

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

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

这篇关于如何检查值是否为Integer类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆