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

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

问题描述

我需要检查一个值是否为整数.我发现了这个:如何检查输入值是否为整数还是浮点数?,但如果我没记错的话,即使 value 本身,变量仍然是 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.

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

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