How can I check if a value is of type Integer?(如何检查值是否为整数类型?)
问题描述
我需要检查一个值是否为整数.我发现了这个:如何检查输入值是否为整数还是浮点数?,但如果我没记错的话,即使 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.
这篇关于如何检查值是否为整数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查值是否为整数类型?


- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01