downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

is_int> <is_double
[edit] Last updated: Sat, 07 Jan 2012

view this page in

is_float

(PHP 4, PHP 5)

is_float변수의 자료형이 소수인지 확인합니다

설명

bool is_float ( mixed $var )

주어진 변수의 자료형이 float인지 확인합니다.

Note:

변수가 숫자나 숫자 문자열(폼 입력 등은 항상 문자열입니다)인지 확인하려면, is_numeric()을 사용해야 합니다.

인수

var

평가할 변수.

반환값

varfloat이면 TRUE 아니면 FALSE를 반환합니다.

예제

Example #1 is_float() 예제

<?php
if(is_float(27.25)) {
 echo 
"is float\n";
} else {
 echo 
"is not float\n";
}
var_dump(is_float('abc'));
var_dump(is_float(23));
var_dump(is_float(23.5));
var_dump(is_float(1e7));  // 과학형식
var_dump(is_float(true));
?>

위 예제의 출력:

is float
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)

참고

  • is_bool() - 변수가 논리형인지 확인
  • is_int() - 변수의 자료형이 정수인지 확인합니다
  • is_numeric() - 변수가 수나 수 문자열인지 확인합니다
  • is_string() - 변수의 자료형이 문자열인지 확인합니다
  • is_array() - 변수가 배열인지 확인
  • is_object() - 변수가 객체인지 확인합니다



is_int> <is_double
[edit] Last updated: Sat, 07 Jan 2012
 
add a note add a note User Contributed Notes is_float
ircadhikari at gmail dot com 14-Mar-2012 06:22
The final version of function to check the actual float value based on previous Kenaniah's version.
<?php
public function isTrueFloat($val){   
       
        if(
is_float($val) || ( (float) $val > (int) $val || strlen($val) != strlen( (int) $val) ) && (ceil($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
// Additional Tested ones
"0.27"      returns true
0.24        returns true
?>
Boylett 20-Sep-2008 09:29
If you want to test whether a string is containing a float, rather than if a variable is a float, you can use this simple little function:

function isfloat($f) return ($f == (string)(float)$f);
ckelley at the ca - cycleworks dot com 14-Jul-2008 03:39
Personally, I use an implicit cast:

if( is_float($value+1) ){
    $value=sprintf("%.2f",$value);
}

Which turns 22.0000000 query result into 22.00 for display to users.
kshegunov at gmail dot com 31-Mar-2008 01:32
As celelibi at gmail dot com stated, is_float checks ONLY the type of the variable not the data it holds!

If you want to check if string represent a floating point value use the following regular expression and not is_float(),
or poorly written custom functions.

/^[+-]?(([0-9]+)|([0-9]*\.[0-9]+|[0-9]+\.[0-9]*)|
(([0-9]+|([0-9]*\.[0-9]+|[0-9]+\.[0-9]*))[eE][+-]?[0-9]+))$/
celelibi at gmail dot com 03-Mar-2008 08:31
Unlike others comment may let you think, this function tests *only* the type of the variable. It does not perform any other test.
If you want to check if a string represents a valid float value, please use is_numeric instead.
WHITE new media architects - Jeroen 10-Jan-2008 03:24
The above printed "is_true_float" function is not correct.
It gives wrong answers on the following tests:
is_true_float("-4,123"); # returns false
is_true_float("0,123");  # returns false

So I changed the function to correctly handle qouted negative floats and quoted floats near zero:

<?php

function is_true_float($mVal)
{
  return (
is_float($mVal)
           || ( (float)
$mVal != round($mVal)
                ||
strlen($mVal) != strlen( (int) $mVal) )
              &&
$mVal != 0 );
}

?>
Kenaniah Cerny 06-Nov-2007 11:21
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 != ) 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 25-Jan-2006 12:08
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);
kirti dot contact at gmail dot com 18-Oct-2005 11:18
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;
 }   
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites