Jump to content

Archived

This topic is now archived and is closed to further replies.

tjmbc

Checking for a whole number.

Recommended Posts

 

I need to check to see if this is a whole number...

 

  $num_in / 5

Share this post


Link to post
Share on other sites

Use the modulus operator:

 

if($num_in % 5 == 0){
    echo 'Multiple of 5';
}else{
    echo 'Not a multiple of 5';
}

Share this post


Link to post
Share on other sites

The input will need to be an integer for that to work .. as long as it is, it should be fine.

Share this post


Link to post
Share on other sites

@doa,

 

if $num_in = 10.00, then you get "not an integer" as the answer is a float (2.00)

Share this post


Link to post
Share on other sites

I must admit, I had to test your code to make sure. You never know, it might catch on.

Share this post


Link to post
Share on other sites

I would have thought it would make sense for is_int to check whether the number is a whole number, regardless of variable type. Or have a seperate function to check if the VALUE is an integer, rather than the TYPE.

 

Modulus never seems to work correctly for me, it's temperomental

Share this post


Link to post
Share on other sites
$myNumber = 23.23;
if(is_float($myNumber))
     echo 'this is a float number';
else
     echo 'this is not a float number';

Share this post


Link to post
Share on other sites

DOA, it does make sense to have a function that checks for integerness even if the value is not an integer, but is_int() isn't that function :)

 

If you write such a function you will have to decide what to do about large floats, which typically aren't integers even if they should be integers, due to floating point innacuracy.

Share this post


Link to post
Share on other sites

Yarp, but that's a problem I can never solve myself :)

 

Anyway, modulus is the answer to the original question - and binary inaccuracy is something we can keep for another day's musing :)

Share this post


Link to post
Share on other sites

Or without calling any functions, you could do:

 

$num = $firstnumber / 2;

if ($num == (int) $num) {

//It's whole

}

else {

//It's not

}

Share this post


Link to post
Share on other sites

It still doesn't allow for floating point inaccuracies. You can't depend on equality tests with FP values.

 

eg your number may be stored as 5.000000001, in which case you need to test if the difference is within tolerable bounds

 

if (abs($num - (int)$num) < 0.000001)

Share this post


Link to post
Share on other sites

Perhaps some examples will help?

 

<?php

$a['int'] =  (int) 4;
$a['float_but_int'] = (float) 4.00;
$a['normal_float'] = (float) 4.1;
$a['small_float'] = (float) 4.0000000000001;
$a['negative_float'] = (float) -5.00004;

foreach ($a as $name => $value) {
echo $name . "'s value is: " . $value . "\n";

if (is_int($value)) {
	echo $name . " is an integer.\n";
}

if (is_float($value)) {
	echo $name . " is a float.\n";
}

if ($value - floor($value) == 0) {
	echo $name . " was determined to be a whole number (integer).\n";
}

if ($value - floor($value) != 0) {
	echo $name . " was determined not to be a whole number (float).\n";
}

echo str_repeat("-", 25) . "\n";
}

 

By subtracting floor($value) from $value and seeing if there is a remainder, you can determine if it's a whole number.

Share this post


Link to post
Share on other sites

hitman6003, I'm not positive, but I think that you may still have floating point innaccuracies with that....

Share this post


Link to post
Share on other sites

maybe, you convert the number to a string, explode() the number, use substr() and check whether past the decimal point is equal to 00 or not.

 

I dunno, worth a try lol

 

Regards ACE

Share this post


Link to post
Share on other sites

hitman6003, I'm not positive, but I think that you may still have floating point inaccuracies with that....

 

I just retried, and discovered that this is the most decimal places it will recognize before rounding:

 

$a['small_float'] = (float) 4.000000000000001;

 

Prior to that it works.

 

It may work with more decimal places on a 64 bit system.

Share this post


Link to post
Share on other sites

Sorry to bump a post from two years ago but when I googled my question this was one of the first links that came up and it didnt solve my answer. This may prove useful to anyone trying to evaluate a whole number.

 

For example I needed to check the quantity a customer was entering within a shopping cart. If it wasn't a true whole number I needed to output an error.

 

Casting (int) doesn't help becuase Hexadecimal values still gets evaluated to its true integer value.

 

////////////////////////////////////////////////////////////////////////////////////////////// 
//is_wholeNumber(string $value)
//Returns TRUE if a WHOLE NUMBER
//Returns FALSE if anything else (Float, String, Hex, etc)
//////////////////////////////////////////////////////////////////////////////////////////////   
function is_wholeNumber($value)
{
if(preg_match ("/[^0-9]/", $value))
{	return FALSE;	}
return TRUE;
}

 

*EDIT*

opps, sorry, forgot credit to original author

http://davidwalsh.name/php-validatie-numeric-digits

Share this post


Link to post
Share on other sites

Since by default all form data is of the string type, you can simply use ctype_digit. No pattern necessary.

Share this post


Link to post
Share on other sites

Ah yes, I should have read his comments, my bad  ::) been a long day  :shy:

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.