Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: is_float - Manual
[go: Go Back, main page]

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

search for in the

is_int" width="11" height="7"/> <is_double
Last updated: Fri, 06 Jun 2008

view this page in

is_float

(PHP 4, PHP 5)

is_float — 変数の型が float かどうか調べる

説明

bool is_float ( mixed $var )

与えられた変数の型が float かどうかを調べます。

注意: 変数が数値もしくは数値形式の文字列の場合 (フォームからの入力の場合は 常に文字列となります) 、is_numeric() を使用する必要があります。

パラメータ

var

評価する変数

返り値

もし varfloat 型 の場合 TRUE、 そうでない場合は FALSE を返します。

例1 is_float() の例

<?php
if(is_float(27.25)) {
 echo 
"float です\n";
}else {
 echo 
"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));
?>

上の例の出力は以下となります。

float です
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)



is_int" width="11" height="7"/> <is_double
Last updated: Fri, 06 Jun 2008
 
add a note add a note User Contributed Notes
is_float
kshegunov at gmail dot com
01-Apr-2008 06: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
04-Mar-2008 01: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 08: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
07-Nov-2007 04: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
26-Jan-2006 05: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
19-Oct-2005 03: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;
 }   
?>

is_int" width="11" height="7"/> <is_double
Last updated: Fri, 06 Jun 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites