Check string is float php

What is the easiest way to check if a string contains a valid float?

For example

is_string_float("1") = true
is_string_float("1.234") = true
is_string_float("1.2e3") = true
is_string_float("1b2") = false
is_string_float("aldhjsfb") = false

asked Dec 5, 2016 at 11:51

Check string is float php

3

The easiest way would be to use built in function is_float(). To test if a variable is a number or a numeric string you must use is_numeric().

answered Dec 5, 2016 at 12:02

1

If you really want to know if a string contains a float and ONLY a float you cannot use is_float() (wrong type) or is_numeric() (returns true for a string like "1" too) only. I'd use


instead.

answered Jul 2, 2019 at 11:50

Lenny EingLenny Eing

1311 silver badge8 bronze badges

maybe you can use a couple of functions

out of the box

function is_string_float($string) {
    if(is_numeric($string)) {
        $val = $string+0;

        return is_float($val);
    } 
      
    return false;
}

Check string is float php

answered Dec 5, 2016 at 11:57

donald123donald123

5,4583 gold badges24 silver badges23 bronze badges

2

This can easily be achieved by double casting.

/**
 * @param string $text
 * @return bool
 */
function is_string_float(string $text): bool
{
    return $text === (string) (float) $text;
}

answered Oct 5, 2021 at 9:35

1

Not the answer you're looking for? Browse other questions tagged php string floating-point or ask your own question.

I am trying to check in php if a string is a double or not.

Here is my code:

   if(floatval($num)){
         $this->print_half_star();
    }

$num is a string..The problem is that even when there is an int it gives true. Is there a way to check if it is a float and not an int!?

asked Aug 7, 2012 at 15:54

1

// Try to convert the string to a float
$floatVal = floatval($num);
// If the parsing succeeded and the value is not equivalent to an int
if($floatVal && intval($floatVal) != $floatVal)
{
    // $num is a float
}

answered Aug 7, 2012 at 16:02

Check string is float php

4

This will omit integer values represented as strings:

if(is_numeric($num) && strpos($num, ".") !== false)
{
    $this->print_half_star();
}

answered Aug 7, 2012 at 16:01

JK.JK.

5,0861 gold badge26 silver badges26 bronze badges

1

You can try this:

function isfloat($num) {
    return is_float($num) || is_numeric($num) && ((float) $num != (int) $num);
}

var_dump(isfloat(10));     // bool(false)
var_dump(isfloat(10.5));   // bool(true)
var_dump(isfloat("10"));   // bool(false)
var_dump(isfloat("10.5")); // bool(true)

answered Aug 7, 2012 at 16:02

FlorentFlorent

12.3k10 gold badges46 silver badges58 bronze badges

1

You could just check if the value is numeric, and then check for a decimal point, so...

if(is_numeric($val) && stripos($val,'.') !== false)
{
    //definitely a float
}

It doesn't handle scientific notation very well though, so you may have to handle that manually by looking for e

answered Nov 28, 2018 at 19:27

Why not use the magic of regular expression

answered Aug 7, 2012 at 16:03

Check string is float php

ARIF MAHMUD RANAARIF MAHMUD RANA

4,8453 gold badges30 silver badges53 bronze badges

2

if "double string format " like this "XXXXXX.XXXXXX"

try check

function check_double_string($str){
 $pairs = explode('.',$str);
 if ( is_array($pairs) && count($pairs)==2) {
   return ( is_numeric($pairs[0]) && is_numeric($pairs[1])? true : false; 
 }
 return false;
}

answered Aug 7, 2012 at 16:05

Check string is float php

voodoo417voodoo417

11.7k3 gold badges34 silver badges39 bronze badges

1

In order to get all cases of a float we've got to add any zero's back on to the end that may be chopped off by floatval() or by typecasting (float)$num

$num='17.000010';
$num_float = (float)$num; //$num_float is now 17.00001  
//add the zero's back to $num_float
while (strlen ($num) > strlen ($num_float))  $num_float = $num_float . '0';   //$spot_float in now a string again


if($num_float != $num) then $num was no double :;

note !== was not used just in case $num_float was never converted back to a string by adding zeroes. such would be the case for values that didn't end in 0.

answered Apr 20, 2016 at 21:34

Here's what I ended up creating, for modern PHP:

/**
 * Determines if a variable appears to be a float or not.
 *
 * @param string|int|double $number
 * @return bool True if it appears to be an integer value. "75.0000" returns false.
 * @throws InvalidArgumentException if $number is not a valid number.
 */
function isFloatLike($number): bool
{
    // Bail if it isn't even a number.
    if (!is_numeric($number)) {
        throw new InvalidArgumentException("'$number' is not a valid number.");
    }

    // Try to convert the variable to a float.
    $floatVal = floatval($number);

    // If the parsing succeeded and the value is not equivalent to an int, 
    return ($floatVal && intval($floatVal) != $floatVal);
}

answered Apr 10, 2019 at 4:34

Theodore R. SmithTheodore R. Smith

20.8k12 gold badges60 silver badges89 bronze badges

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

Is float in PHP example?

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

How does PHP calculate float value?

Dealing with floating-point calculations in PHP.
1 – work on Integers. echo intval((0.1*10) + (0.7*10)); // int(8) ... .
2 – the round() function. echo intval(round((0.1+0.7)*10, 0)); // int(8) ... .
3 – casting to string. echo intval(strval((0.1+0.7)*10)); // int(8) ... .
4 – BC Math. ... .
Summary..

Is float a value?

Integers and floats are two different kinds of numerical data. An integer (more commonly called an int) is a number without a decimal point. A float is a floating-point number, which means it is a number that has a decimal place. Floats are used when more precision is needed.