tjmbc Posted May 22, 2008 Share Posted May 22, 2008 I need to check to see if this is a whole number... $num_in / 5 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 22, 2008 Share Posted May 22, 2008 Use the modulus operator: if($num_in % 5 == 0){ echo 'Multiple of 5'; }else{ echo 'Not a multiple of 5'; } Quote Link to comment Share on other sites More sharing options...
btherl Posted May 23, 2008 Share Posted May 23, 2008 The input will need to be an integer for that to work .. as long as it is, it should be fine. Quote Link to comment Share on other sites More sharing options...
deadonarrival Posted May 25, 2008 Share Posted May 25, 2008 $answer = $num_in / 5; if(is_int($answer)) { #integer } else { #not an integer } Quote Link to comment Share on other sites More sharing options...
Barand Posted May 25, 2008 Share Posted May 25, 2008 @doa, if $num_in = 10.00, then you get "not an integer" as the answer is a float (2.00) Quote Link to comment Share on other sites More sharing options...
deadonarrival Posted May 25, 2008 Share Posted May 25, 2008 Hmm, didn't know that one. Ignore my suggestion then Quote Link to comment Share on other sites More sharing options...
Barand Posted May 25, 2008 Share Posted May 25, 2008 I must admit, I had to test your code to make sure. You never know, it might catch on. Quote Link to comment Share on other sites More sharing options...
deadonarrival Posted May 26, 2008 Share Posted May 26, 2008 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 Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 27, 2008 Share Posted May 27, 2008 $myNumber = 23.23; if(is_float($myNumber)) echo 'this is a float number'; else echo 'this is not a float number'; Quote Link to comment Share on other sites More sharing options...
btherl Posted May 27, 2008 Share Posted May 27, 2008 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. Quote Link to comment Share on other sites More sharing options...
deadonarrival Posted May 27, 2008 Share Posted May 27, 2008 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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 7, 2008 Share Posted June 7, 2008 Or without calling any functions, you could do: $num = $firstnumber / 2; if ($num == (int) $num) { //It's whole } else { //It's not } Quote Link to comment Share on other sites More sharing options...
Barand Posted June 8, 2008 Share Posted June 8, 2008 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) Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted June 13, 2008 Share Posted June 13, 2008 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted June 13, 2008 Share Posted June 13, 2008 hitman6003, I'm not positive, but I think that you may still have floating point innaccuracies with that.... Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted June 13, 2008 Share Posted June 13, 2008 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 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 14, 2008 Share Posted June 14, 2008 Or maybe you just stick with Barand's solution. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted June 15, 2008 Share Posted June 15, 2008 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. Quote Link to comment Share on other sites More sharing options...
berridgeab Posted January 7, 2011 Share Posted January 7, 2011 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2011 Share Posted January 7, 2011 Since by default all form data is of the string type, you can simply use ctype_digit. No pattern necessary. Quote Link to comment Share on other sites More sharing options...
berridgeab Posted January 7, 2011 Share Posted January 7, 2011 Ah yes, I should have read his comments, my bad been a long day Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.