Jump to content

Checking for a whole number.


tjmbc

Recommended Posts

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

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.

  • 2 weeks later...

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)

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.

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.

  • 2 years later...

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

Archived

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

×
×
  • 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.