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";
How would I do this?
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";
You are using int so it will always be a "whole" number. But in case you are using a double then you can do something like this
double foobar = something;
if(foobar == static_cast<int>(foobar))
return true;
else
return false;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With