I need to check if a variable is an integer, for example, I have code:
double foobar = 3; //Pseudocode if (foobar == whole) cout << "It whole"; else cout << "Not whole";
How can I do it?
Assuming foobar is a floating point value, you can round it and compare with the number itself:
foobar
if (floor(foobar) == foobar) cout << "It whole"; else cout << "Not whole";
You use int, so it will always be an integer. But if you use double, you can do something like this
double foobar = something; if(foobar == static_cast<int>(foobar)) return true; else return false;
Depends on your definition of an integer. If you consider only 0 and above an integer, then this is simple: bool whole = foobar >= 0; .
bool whole = foobar >= 0;
just write function or expression to check the whole number , returning bool .
function
expression
whole number
bool
in the usual definition, I think the integer is greater than 0 without the decimal part.
then
if (abs(floor(foobar) )== foobar) cout << "It whole"; else cout << "Not whole";
Source: https://habr.com/ru/post/910224/More articles:How to actually see a bitmap taken from a dump of Android heap - androidhow to read image files and store them in memory (std :: string) in c ++? - c ++adaptive indentation in emacs? - indentationDoes the Facebook API have a graphic design? - apiCan redis pipeline multiple commands that depend on previous ones? - databaseLinux tools for reading / flushing MachO files? - linuxAssign after decompression - xsltC # bitmap object, color looks transparent - c #Remove MySQL completely from Ubuntu - mysqlObtaining percentage scales reflecting individual faces with ggplot2 - rAll Articles