16

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?

2
  • whoops sorry, meant double, just edited
    – Billjk
    Mar 8, 2012 at 4:24
  • 2
    I think you have an XY problem: you have problem X and potential solution Y, so you’re asking about the latter though you should be asking about the former.
    – Jon Purdy
    Mar 8, 2012 at 4:35

9 Answers 9

24

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";
1
  • There is nothing to round down in an integer. Mar 8, 2012 at 4:23
5

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;
3

The answer of laurent is great, here is another way you can use without the function floor

#include <cmath> // fmod

bool isWholeNumber(double num)
{
  reture std::fmod(num, 1) == 0;
  // if is not a whole number fmod will return something between 0 to 1 (excluded)
}

fmod function

1

All you have to do is define your possible decimal number as an int and it will automatically round it, then compare the double with the int. For example, if your double foobar is equal to 3.5, defining it as an int will round it down to 3.

double foobar = 3;
long long int num = foobar;

if (foobar == num) {
  //whole
} else {
  //not whole
}
2
  • 2
    a double can represent more whole numbers than a 32 bit int, you you should at least use a long long int
    – Unlikus
    Sep 16, 2020 at 17:09
  • I hardly see how that's related at all, @Unlikus, but if you insist, I will edit it. The OP's example was 3, so I didn't think I needed a long long int.
    – avisk
    Sep 18, 2020 at 16:17
1

In C++ you can use the following code:

if (foobar - (int)foobar == 0.0 && foobar>=0)
cout << "It's whole";
else
cout << "Not whole";
1
if (foobar == (int)foobar)
    cout << "It's whole";
else
    cout << "Not whole";
0

just write a function or expression to Check for whole number, returning bool.

in usual definition i think whole number is greater than 0 with no decimal part.

then,

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

Depends on your definition of whole number. If you consider only 0 and above as whole number then it's as simple as: bool whole = foobar >= 0;.

2
  • @downvoter: The definition of whole number is not precise. See wikipedia: en.wikipedia.org/wiki/Whole_number .
    – Asha
    Mar 8, 2012 at 4:27
  • huh, never heard of that. Every time its come up, everyone I talked to agreed on the definition. Mar 8, 2012 at 15:25
-1

A concise version of Pepe's answer

bool isWhole(double num)
{
   return num == static_cast<int>(num);
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.