15

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?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Billjk
  • 10,387
  • 23
  • 54
  • 73
  • whoops sorry, meant double, just edited – Billjk Mar 08 '12 at 04: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 08 '12 at 04:35

9 Answers9

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";
laurent
  • 88,262
  • 77
  • 290
  • 428
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;
Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
Pepe
  • 6,360
  • 5
  • 27
  • 29
2

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
}
avisk
  • 330
  • 2
  • 6
  • 16
  • 1
    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 '20 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 '20 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";
suyashpatil
  • 236
  • 3
  • 11
1
if (foobar == (int)foobar)
    cout << "It's whole";
else
    cout << "Not whole";
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
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";
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
  • 1
    Note: there is a rejected "too radical" suggested edit: http://stackoverflow.com/review/suggested-edits/5928097 – Vi. Oct 05 '14 at 23:34
-1

A concise version of Pepe's answer

bool isWhole(double num)
{
   return num == static_cast<int>(num);
}
Moia
  • 2,216
  • 1
  • 12
  • 34
-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;.

Asha
  • 11,002
  • 6
  • 44
  • 66
  • @downvoter: The definition of whole number is not precise. See wikipedia: http://en.wikipedia.org/wiki/Whole_number . – Asha Mar 08 '12 at 04:27
  • huh, never heard of that. Every time its come up, everyone I talked to agreed on the definition. – Mooing Duck Mar 08 '12 at 15:25