有没有办法检查一个变量是否是一个整数? C ++ [英] Is there a way to check if a variable is a whole number? C++

查看:139
本文介绍了有没有办法检查一个变量是否是一个整数? C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查一个变量是否是一个整数,比如我有代码:

I need to check if a variable is a whole number, say I have the code:

double foobar = 3;
//Pseudocode
if (foobar == whole)
    cout << "It's whole";
else
    cout << "Not whole";

我该如何做?

推荐答案

假设 foobar 实际上是一个浮点值,您可以舍入它并将其与数字本身进行比较:

Assuming foobar is in fact a floating point value, you could round it and compare that to the number itself:

if (floor(foobar) == foobar)
    cout << "It's whole";
else
    cout << "Not whole";

这篇关于有没有办法检查一个变量是否是一个整数? C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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