Is float function in php?

❮ PHP Variable Handling Reference

Example

Return the float value of different variables:

Try it Yourself »

Definition and Usage

The floatval[] function returns the float value of a variable.

Syntax

Parameter Values

ParameterDescription
variable Required. Specifies the variable to check. Must be a scalar type

Technical Details

Return Value:Return Type:PHP Version:
The float value of the variable on success, 0 on failure. An empty array will return 0, and a non-empty array will return 1
Float
4.2+

❮ PHP Variable Handling Reference


[PHP 4, PHP 5, PHP 7, PHP 8]

is_floatFinds whether the type of a variable is float

Description

is_float[mixed $value]: bool

Note:

To test if a variable is a number or a numeric string [such as form input, which is always a string], you must use is_numeric[].

Parameters

value

The variable being evaluated.

Return Values

Returns true if value is a float, false otherwise.

Examples

Example #1 is_float[] example

var_dump[is_float[27.25]];
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]];
?>

The above example will output:

bool[true]
bool[false]
bool[false]
bool[true]
bool[true]
bool[false]

See Also

  • is_bool[] - Finds out whether a variable is a boolean
  • is_int[] - Find whether the type of a variable is integer
  • is_numeric[] - Finds whether a variable is a number or a numeric string
  • is_string[] - Find whether the type of a variable is string
  • is_array[] - Finds whether a variable is an array
  • is_object[] - Finds whether a variable is an object

Anonymous

1 year ago

is_float[] returns true for NAN, INF and -INF. You may want to test is_float[$value] && is_finite[$value], or alternatively filter_var[$value, FILTER_VALIDATE_FLOAT] !== false.

nonzer0value

7 years ago

Coercing the value to float and back to string was a neat trick. You can also just add a literal 0 to whatever you're checking.



Seems to work ok:



YMMV

Boylett

14 years ago

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];

Erutan409 at Hotmail dot com

7 years ago

Boylett's solution is elegant [//php.net/manual/en/function.is-float.php#85848], but won't work for long float's or variables that are not explicitly type of 'string' or for long floats that are encased in quotes, making it a string that will be truncated/rounded when cast to a float.  So, further logic must be completed to test for the case.  Take the following example:



Will produce [32-bit]:

string[34] "3.14159265358979323846264338g32795"
float[3.1415926535898]
bool[false]
bool[false]

So far, so good, right?  Yeah, but it's misleading, because the string is so long, that when it's converted to a float, it won't be equivalent to the comparison of the value being cast back into a string .  So the aforementioned short function works.  Look at this next example:



Will produce [32-bit]:

float[3.1415926535898]
float[3.1415926535898]
bool[false]
bool[true]

Why is it not working now, but the value is truly a float?  Same reasoning as mentioned before.  The float is so long that it's truncated/rounded and doesn't match the comparison being done with the short-hand function.

So, as you can see, more logic should be applied to the variable you're testing.

kshegunov at gmail dot com

14 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]+]]$/

KIVagant at gmail dot com

10 years ago

Yet another regular expression for float in real life:



// 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'

Mitch

10 years 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.

phper

16 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];

ivan dot maruca at gmail dot com

6 years ago

check if string/numeric is float or not :

ckelley at the ca - cycleworks dot com

14 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.

Does PHP have float?

In PHP, the Float data type is used to set fractional values. A float is a number with a decimal point and can be extended to exponential form. Float is also called a floating-point number. Various ways to represent float values are 3.14, 4.75, 5.88E+20, etc.

What is float data type PHP?

PHP Floats A float is a number with a decimal point or a number in exponential form. 2.0, 256.4, 10.358, 7.64E+5, 5.56E-5 are all floats. The float data type can commonly store a value up to 1.7976931348623E+308 [platform dependent], and have a maximum precision of 14 digits.

How do you declare a variable as float in PHP?

Floating point numbers [also known as "floats", "doubles", or "real numbers"] can be specified using any of the following syntaxes: so defining a float by using $var = 0.0 is correct.

How check value is decimal or not in PHP?

The is_numeric[] function in the PHP programming language is used to evaluate whether a value is a number or numeric string. Numeric strings contain any number of digits, optional signs such as + or -, an optional decimal, and an optional exponential. Therefore, +234.5e6 is a valid numeric string.

Chủ Đề