...

View Full Version : is_float



timgolding
07-02-2007, 04:31 PM
I am trying to validate a float. I need to test if a string variable is a float. i tried the is_float function but it doesnt seem to work with string variables



$duration=$_POST['duration'];

if(!is_float($duration))
{
die ('<div style="clear:both;float:left;"><h3>Sorry your duration must be a number in hours.</h3>
<table><tr><td>Click here to go back -</td><td> <a href="?area=1" class="button" >Back </a></td></tr></table></div></div>');
}

whizard
07-02-2007, 05:04 PM
is_float doesn't work with strings.

Try converting $duration to an integer first using the intval() function.

HTH
Dan

timgolding
07-02-2007, 05:41 PM
Hi thanks for the suggestion. However will converting my string in to an interger truncate the decimal digits? As the user may have inputed 1.5 as the duration.

PappaJohn
07-02-2007, 05:43 PM
You can use floatval() to convert it to a float.

GJay
07-02-2007, 05:47 PM
floatval() will turn anything (scalar) into a float, so probably not very useful:


var_dump(is_float(floatval('hello')));
//output=bool(true)


will is_numeric() not do what you want? (http://php.net/is-numeric)

timgolding
07-02-2007, 05:50 PM
Thanks GJay thats the code i was looking for and now i look its right there on php.net silly me thanks guys :-)

whizard
07-02-2007, 11:24 PM
Hi thanks for the suggestion. However will converting my string in to an interger truncate the decimal digits? As the user may have inputed 1.5 as the duration.


oh my bad, I was thinking of int as meaning any number value, sorry!

Dan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum