PHP: convert string to float (only if string represents a float)

前端 未结 5 939
隐瞒了意图╮
隐瞒了意图╮ 2021-01-16 08:59

I have an unknwon string that could resemble a float. In that case I want to convert it to float (for calculations), otherwise leave it as a string.

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-16 09:11

    maybe you would like to use the non-locale-aware floatval function:

    float floatval ( mixed $var ) - Gets the float value of a string.

    Example from the documentation:

    $string = '122.34343The';
    $float  = floatval($string);
    echo $float; // 122.34343
    

提交回复
热议问题