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);
is_float
(PHP 4, PHP 5)
is_float — Informa se a variável é do tipo float
Descrição
Verifica se a dada variável é do tipo ponto flutuante.
Nota:
Para verificar se a variável é um número ou uma string numérica (campos de formulários sempre são strings númericas), use is_numeric().
Parâmetros
-
var -
A variável a ser avaliada.
Valor Retornado
Retorna TRUE se var é float,
FALSE caso contrário.
Exemplos
Exemplo #1 Exemplo da 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)); //Scientific Notation
var_dump(is_float(true));
?>
O exemplo acima irá imprimir:
is float bool(false) bool(false) bool(true) bool(true) bool(false)
Veja Também
- is_bool() - Verifica se a variável é um boleano
- is_int() - Informa se a variável é do tipo inteiro
- is_numeric() - Informa se a variável é um número ou uma string numérica
- is_string() - Informa se a variável é do tipo string
- is_array() - Verifica se a variável é um array
- is_object() - Informa se a variável é um objeto
Boylett ¶
4 years ago
phper ¶
7 years ago
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);
kshegunov at gmail dot com ¶
5 years ago
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]+))$/
Mitch ¶
11 months ago
Another RegEx for matching floats:
/^[+-]?(\d*\.\d+([eE]?[+-]?\d+)?|\d+[eE][+-]?\d+)$/
Matches:
1e2
+1353.0316547
13213.032468e-13465
-8E+3
-1354.98879e+37436
Non-matches:
8
e2
-e-4
E
asdf.safd
8e
Enjoy.
ckelley at the ca - cycleworks dot com ¶
4 years ago
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.
KIVagant at gmail dot com ¶
8 months ago
Yet another regular expression for float in real life:
<?php
function isTrueFloat($val)
{
$pattern = '/^[-+]?(((\\\\d+)\\\\.?(\\\\d+)?)|\\\\.\\\\d+)([eE]?[+-]?\\\\d+)?$/';
return (!is_bool($val) && (is_float($val) || preg_match($pattern, trim($val))));
}
?>
// Matches:
1, -1, 1.0, -1.0, '1', '-1', '1.0', '-1.0', '2.1', '0', 0, ' 0 ', ' 0.1 ', ' -0.0 ', -0.0, 3., '-3.', '.27', .27, '-0', '+4', '1e2', '+1353.0316547', '13213.032468e-13465', '-8E+3', '-1354.98879e+37436'
// Non-matches:
false, true, '', '-', '.a', '-1.a', '.a', '.', '-.', '1+', '1.3+', 'a1', 'e.e', '-e-4', 'e2', '8e', '3,25'
yo_llo at gmail dot com ¶
3 months ago
To check if a variable is a floating string type:
<?php
$var = "1.2";
if (is_numeric ($var) && fmod ((float) $var, 1) !== 0) echo "float";
?>
kirti dot contact at gmail dot com ¶
7 years ago
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;
}
?>
celelibi at gmail dot com ¶
5 years ago
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.
