|
is_float
Finds whether the type of a variable is float
(PHP 4, PHP 5)
Example 2591. is_float() example<?php The above example will output: is float Code Examples / Notes » is_floatkirti dot contact
To check a float only should contain certain number of decimal places, I have used this simple function below <? function is_deccount($number,$decimal=2){ $m_factor=pow(10,$decimal); if((int)($number*$m_factor)==$number*$m_factor) return true; else return false; } ?> kenaniah cerny
For those of you who have discovered that is_float() does not behave exactly the way you would expect it to when passing a string, here is a function that extends is_float properly report floating numbers given any sort of mixed variable. <?php function is_true_float($val){ if( is_float($val) || ( (float) $val > (int) $val || strlen($val) != strlen( (int) $val) ) && (int) $val != 0 ) return true; else return false; } ?> <?php //Tests '4.0' returns true '2.1' returns true 0 returns false "0" returns false 3. returns true 13 returns false "12" returns false 3.53 returns true ?> Enjoy phper
A better way to check for a certain number of decimal places is to use : $num_dec_places = 2; number_format($value,$num_dec_places); |
Change Languagedebug_zval_dump doubleval empty floatval get_defined_vars get_resource_type gettype import_request_variables intval is_array is_binary is_bool is_buffer is_callable is_double is_float is_int is_integer is_long is_null is_numeric is_object is_real is_resource is_scalar is_string is_unicode isset print_r serialize settype strval unserialize unset var_dump var_export |