Author Topic: Checking for a whole number.  (Read 14523 times)

0 Members and 1 Guest are viewing this topic.

Offline MasterACE14

  • Addict
  • Posts: 2,645
  • Gender: Male
  • Programming, the art of combining math and logic.
    • View Profile
    • Crikey Games Pty Ltd
Re: Checking for a whole number.
« Reply #15 on: June 13, 2008, 06:52:28 PM »
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
Why is hashing a hash bad practice?
Quote from: Zane
A business person’s best customers are always using the cheapest piece of shit computer out there. I don’t have the sources to back it up, but I’d like to say it’s a proven fact.
Quote from: requinix
Use objects when they make sense and functions when they don't.
Crikey Games Pty Ltd | Realm Battles Classic
It's too big a world to be in competition with everyone.  The only person who I have to be better than is myself. ~Harry Morgan

Offline GingerRobot

  • Guru
  • Fanatic
  • *
  • Posts: 4,133
  • Gender: Male
  • Call me Ben
    • View Profile
Re: Checking for a whole number.
« Reply #16 on: June 14, 2008, 01:59:10 AM »
Or maybe you just stick with Barand's solution.

Offline hitman6003

  • Addict
  • Posts: 2,543
    • View Profile
Re: Checking for a whole number.
« Reply #17 on: June 14, 2008, 08:52:39 PM »
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:

Code: [Select]
$a['small_float'] = (float) 4.000000000000001;
Prior to that it works.

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

Offline berridgeab

  • Enthusiast
  • Posts: 54
    • View Profile
Re: Checking for a whole number.
« Reply #18 on: January 07, 2011, 09:05:43 AM »
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
« Last Edit: January 07, 2011, 09:08:06 AM by berridgeab »

Offline Pikachu2000

  • I hate everything.
  • Global Moderator
  • Freak!
  • *
  • Posts: 9,062
  • Gender: Male
  • Is it solipsistic in here, or is it just me?
    • View Profile
Re: Checking for a whole number.
« Reply #19 on: January 07, 2011, 09:16:00 AM »
Since by default all form data is of the string type, you can simply use ctype_digit(). No pattern necessary.
"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf * || Linux Help --> linuxforum.com

Offline berridgeab

  • Enthusiast
  • Posts: 54
    • View Profile
Re: Checking for a whole number.
« Reply #20 on: January 07, 2011, 09:20:25 AM »
Ah yes, I should have read his comments, my bad   ::) been a long day  :shy: