Most people learn how to figure out if a number is prime since it’s a beginning programming staple. A slightly more uncommon numerical programming challenge might be determining if a number is a whole number.

A whole number is any number with no fractional parts. For instance, **19** is a whole number while **19.95** is not. In the latter case, the **.95** is the fractional or decimal part of the number. As a human, you can recognize instantly what is and what is not a whole number. You might be doing it by looking at the *decimal point* or by some other crazy means.

### Code

I used Java Script for the examples.

Following the human method, one could simply parse out a decimal point to test if a number is whole.

Ã¢â‚¬â€¹var notwhole = 19.95; var whole = 19; Ã¢â‚¬â€¹function isWhole_string(number) { var str = "" + number; str = str.split("."); return ( str > 0 ? true : false ); }

Basically, if the number’s string has more than one array part, it is a whole number.

The problem with the string solution is that exactly: it relies on a string to do the dirty work. There should be a better and more mathematical solution to the problem. I turned to rounding. For instance, if you compare 19.95 to 19, you’ll find they are not equal to each other. This assumes you have a whole number to work with then. Fine.

// using notwhole, whole function isWhole_rounding(number) { var rounded = Math.round(number); return rounded == number; }

This method looks better. It doesn’t rely on dirty string trickery to do its job. Let’s break it down anyway, just in case. First, we store a rounded version of *number* and finally we return the comparison of rounded versus number. If the numbers were already whole (e.g. `19 == 19`

) it would return true. However, if one was not whole (e.g. `19.95 == 19`

), it’ll fail. It’s simple and easy. One thing worries me though. I don’t know how **Math.round** works. In other words, I don’t know the algorithm. Rounding algorithms often rely on internal methods that are a total mystery as to their implementation. So, the search continues.

As you doubtlessly know, the modulus function takes the remainder of division. For instance, **152 / 19 = 8, r = 0**. You can see that the remainder is zero. A different result can happen though, when the the numbers don’t divide evenly. For instance, **152 / 18 = 8.4444** The fractional part indicates that the division wasn’t exact and that some whole parts had to be split up. If we included the remainder though: `152 / 8 = 8, r = 8`

. Now the remainder is 8. This modulus functionality comes in handy to test if a number is even: `number % 2 == 0`

and of course the inverse, odd: `!(number % 2 == 0)`

. Wouldn’t it be great if it could even test for wholeness?

The solution I like best right now relies on the modulus function. Here it is.

// using notewhole, whole function isWhole_modulus(number) { return ( number % 1 > 0 ? false : true ); }Ã¢â‚¬â€¹

The trend is getting the function as short as possible. Besides that, this method relies on no functions, except the modulus operator (which might as well be called a function). Let’s see why this works. When you take `152 / 1`

, you get `152`

. It’s the same with a fractional part too, `152.1 / 1`

, you get `152.1`

. That’s no big deal. What if you’re looking for the remainder after dividing by 1? When you try `152 % 1`

, you get `0`

. There is no remainder. But then, using `152.1 / 1`

, you get something like `0.09999999999999432`

. There really isn’t a good reason this happens of course, but it works everywhere modulus works.

### Conclusion

The string method is a mess, the rounding method is irksome because you don’t control the code and finally, the modulus method is platform independent and for the most part, purely mathematical.

You can see my demos live in jsFiddle. There is also a new updated demo page that allows other solutions to be easily included and tested.

What are your ideas for determining if a number is a whole number?