Jump to content

Check is the input value is a double (float) value


haris244808

Recommended Posts

Hi there,

 

im trying to validate an input if its float or not...

As im getting the value with $_POST['smth']; the value its considered as string, so is_float() function doesnt work. I tried to convert the string also with floatval() function that doesnt work either

 

Is there any suggestion on how to get done with this??

 

THNX

  Quote
As im getting the value with $_POST['smth']; the value its considered as string, so is_float() function doesnt work. I tried to convert the string also with floatval() function that doesnt work either

What makes you think this?  That's exactly the purpose of those functions:

 

 

php > $a = "12.34";
php > var_dump(is_float($a));
bool(false)
php > var_dump(floatval($a));
float(12.34)
php > var_dump(floatval($a) == $a);
bool(true)
php > 

 

  Quote

  Quote
As im getting the value with $_POST['smth']; the value its considered as string, so is_float() function doesnt work. I tried to convert the string also with floatval() function that doesnt work either

What makes you think this?  That's exactly the purpose of those functions:

 

 

php > $a = "12.34";
php > var_dump(is_float($a));
bool(false)
php > var_dump(floatval($a));
float(12.34)
php > var_dump(floatval($a) == $a);
bool(true)
php > 

 

 

yes i know thaose functions works well ..

But as im getting input with $_POST[]... it seems that it gets as a string so when i check, it doesn work :

here is the code:

 

if(!is_float($this->place_latitude) || !is_float($this->place_longitude)){

$this->errors[] = "The Latitude and Longitude must be a number(double)";

return false;

}

 

here is the html form for only lattitude:

<td><label>Longitude: </label></td>

<td><input name="p_latitude" type="text" placeholder="Place latitude"></td>

 

and i get it with: $newPlace->place_latitude = trim($_POST['p_latitude']);

 

so when i type a numer (ex: 12321) which in this case is an integer. It returns true instead of showing the error mesage (returning false)

 

  Quote

yes i know thaose functions works well ..

But as im getting input with $_POST[]... it seems that it gets as a string so when i check, it doesn work :

 

Please reread ManiacDan's response. Don't just skim over it - really read it. And pay particular attention to the code examples.

  Quote

  Quote

yes i know thaose functions works well ..

But as im getting input with $_POST[]... it seems that it gets as a string so when i check, it doesn work :

 

Please reread ManiacDan's response. Don't just skim over it - really read it. And pay particular attention to the code examples.

 

i red it well man:

 

php > $a = "12.34";

php > var_dump(is_float($a));

bool(false)

 

as u can see if there is a float in string qoute it returns false...

This is what im saying...even when u write a float in ur input gettinh it with$_POST[] , it gets as a string so it doesnt work...

if u dont believe try the code i gave to u?

 

or try this for simplicity:

$test = "132.3";

if(!is_float($test)){

echo " Fail";}

else{

echo "Pass";}

 

it seems that i frist need to convert the string to float than check it

 

  Quote
This is what im saying...even when u write a float in ur input gettinh it with$_POST[] , it gets as a string so it doesnt work...

if u dont believe try the code i gave to u?

[...]

it seems that i frist need to convert the string to float than check it

We know.  We know very very well.  That's why my code example was more than one line.  It was, in fact, three lines.  One of those lines was how you can check to see if a string is a valid floatvalue.

 

 

also note the existence of is_numeric(), which works on strings.

 

Also also note that latitude and longitude are correctly expressed in minutes and seconds, as in -64'38"

 

  Quote

  Quote
This is what im saying...even when u write a float in ur input gettinh it with$_POST[] , it gets as a string so it doesnt work...

if u dont believe try the code i gave to u?

[...]

it seems that i frist need to convert the string to float than check it

We know.  We know very very well.  That's why my code example was more than one line.  It was, in fact, three lines.  One of those lines was how you can check to see if a string is a valid floatvalue.

 

 

also note the existence of is_numeric(), which works on strings.

 

Also also note that latitude and longitude are correctly expressed in minutes and seconds, as in -64'38"

 

yeah yeah i know that ur code was in three lines:

 

$test = "132.3";

if(var_dump(floatval($test)) != $test){

echo " Fail";}

else{

echo "Pass";}

 

is_numeric() it just checks if its a number or not, so in integers also itll return true (i need to check if its double )

 

for lat and long im getting double values to display the propper postion in map

  Quote
is_numeric() it just checks if its a number or not, so in integers also itll return true (i need to check if its double )

Check the third line.  Try to understand what it's doing.  Combine that with a similar check for intval. 

 

Also note that your code shouldn't contain var_dump, var_dump is an output function that I used to show you results.

haris244808,

 

Have you ever heard about the saying

  Quote
Give a man a fish and feed him for a day. Teach a man how to fish and feed him for a lifetime.

 

A big part of this forum, IMO, is not just providing an answer but providing information that helps users to understand how to think differently about problems they face to understand how to solve problems on their own in the future. You have been given the solution you need. It just wasn't spoon fed to you.

 

Also, every integer is a float, so in addition to the first solution ManiacDan provided you need to pay attention to his last post as well.

  Quote

Also, every integer is a float, so in addition to the first solution ManiacDan provided you need to pay attention to his last post as well.

Integers are not floats internally.  Declaring $a = 12 makes $a an int, not a float.

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.