floatval

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

floatvalRestituisce il valore di una variabile di tipo float

Descrizione

function floatval(mixed $var): float

La funzione restituisce il valore di tipo float di var.

Elenco dei parametri

var

Può anche essere di tipo scalare. La funzione floatval() non dovrebbe essere usata sugli oggetti, facendolo emetterà un errore di livello E_NOTICE e restituirà 1.

Valori restituiti

Il valore in virgola mobile della variabile. Gli array vuoti restituiscono 0, gli array non vuoti restituiscono 1.

Le stringhe molto probabilmente restituiscono 0 anche se questo dipende dai caratteri più a sinistra della stringa. Si applicano le regole comuni del casting del float.

Esempi

Example #1 Esempio di uso di floatval()

<?php
$var = '122.34343The';
$float_value_of_var = floatval($var);
echo $float_value_of_var; // 122.34343
?>

Example #2 Esempio di floatval() con caratteri più a sinistra non numerici

<?php
$var = 'The122.34343';
$float_value_of_var = floatval($var);
echo $float_value_of_var; // 0
?>

Vedere anche:

  • boolval() - Get the boolean value of a variable
  • intval() - Estrae il valore intero da una variabile
  • strval() - Restituisce il valore di una variabile interpretato come stringa
  • settype() - Definisce il tipo di una variabile
  • Type juggling

add a note

User Contributed Notes 22 notes

up
124
brewal dot renault at gmail dot com
12 years ago
This function takes the last comma or dot (if any) to make a clean float, ignoring thousand separator, currency or any other letter :

function tofloat($num) {
    $dotPos = strrpos($num, '.');
    $commaPos = strrpos($num, ',');
    $sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos : 
        ((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);
   
    if (!$sep) {
        return floatval(preg_replace("/[^0-9]/", "", $num));
    } 

    return floatval(
        preg_replace("/[^0-9]/", "", substr($num, 0, $sep)) . '.' .
        preg_replace("/[^0-9]/", "", substr($num, $sep+1, strlen($num)))
    );
}

$num = '1.999,369€';
var_dump(tofloat($num)); // float(1999.369)
$otherNum = '126,564,789.33 m²';
var_dump(tofloat($otherNum)); // float(126564789.33)

Demo : http://codepad.org/NW4e9hQH
up
57
Anonymous
21 years ago
you can also use typecasting instead of functions:

(float) $value;
up
22
Alexey M
10 years ago
There is much easier way to deal with formatted numbers:

<?php
$str = '13,232.95';
$var = (double)filter_var($str, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
var_dump($var);
?>
double(13232.95)
up
25
PapaPinguoin
14 years ago
To view the very large and very small numbers (eg from a database DECIMAL), without displaying scientific notation, or leading zeros.

FR : Pour afficher les très grand et très petits nombres (ex. depuis une base de données DECIMAL), sans afficher la notation scientifique, ni les zéros non significatifs.

<?php
function floattostr( $val )
{
    preg_match( "#^([\+\-]|)([0-9]*)(\.([0-9]*?)|)(0*)$#", trim($val), $o );
    return $o[1].sprintf('%d',$o[2]).($o[3]!='.'?$o[3]:'');
}
?>

<?php
echo floattostr("0000000000000001");
echo floattostr("1.00000000000000");
echo floattostr("0.00000000001000");
echo floattostr("0000.00010000000");
echo floattostr("000000010000000000.00000000000010000000000");
echo floattostr("-0000000000000.1");
echo floattostr("-00000001.100000");

// result
// 1
// 1
// 0.00000000001
// 0.0001
// 10000000000.0000000000001
// -0.1
// -1.1

?>
up
20
anonymous at start dot be
22 years ago
Easier-to-grasp-function for the ',' problem.

<?php
function Getfloat