我想将这些类型的值“3”、“2.34”、“0.234343”等转换为数字。在JavaScript中,我们可以使用Number(),但在PHP中有没有类似的方法?

Input             Output
'2'               2
'2.34'            2.34
'0.3454545'       0.3454545

当前回答

下面是我为自己编写的一个函数:

它还返回布尔、整数、双精度和实数的简写版本。

function type($mixed, $parseNumeric = false)
{        
    if ($parseNumeric && is_numeric($mixed)) {
        //Set type to relevant numeric format
        $mixed += 0;
    }
    $t = gettype($mixed);
    switch($t) {
        case 'boolean': return 'bool'; //shorthand
        case 'integer': return 'int';  //shorthand
        case 'double': case 'real': return 'float'; //equivalent for all intents and purposes
        default: return $t;
    }
}

调用parseNumeric设置为true的类型将在检查类型之前转换数字字符串。

因此:

type(“5”,true)将返回int

类型(“3.7”,true)将返回float

类型(“500”)将返回字符串

只是要小心,因为这是一种错误检查方法,而实际变量仍然是字符串。如果需要,您需要将实际变量转换为正确的类型。我只需要它来检查数据库是否应该加载项id或别名,这样就不会产生任何意外的影响,因为它在运行时将被解析为字符串。

Edit

如果要检测对象是否为函数,请在开关中添加以下情况:

case 'object': return is_callable($mixed)?'function':'object';

其他回答

所有建议都会丢失数字类型。

在我看来,这是一个最佳实践:

function str2num($s){
// Returns a num or FALSE
    $return_value =  !is_numeric($s) ? false :               (intval($s)==floatval($s)) ? intval($s) :floatval($s);
    print "\nret=$return_value type=".gettype($return_value)."\n";
}

PHP将在限制范围内为您提供

<?php
   $str = "3.148";
   $num = $str;

   printf("%f\n", $num);
?>

要避免出现问题,请尝试intval($var)。一些示例:

<?php
echo intval(42);                      // 42
echo intval(4.2);                     // 4
echo intval('42');                    // 42
echo intval('+42');                   // 42
echo intval('-42');                   // -42
echo intval(042);                     // 34 (octal as starts with zero)
echo intval('042');                   // 42
echo intval(1e10);                    // 1410065408
echo intval('1e10');                  // 1
echo intval(0x1A);                    // 26 (hex as starts with 0x)
echo intval(42000000);                // 42000000
echo intval(420000000000000000000);   // 0
echo intval('420000000000000000000'); // 2147483647
echo intval(42, 8);                   // 42
echo intval('42', 8);                 // 34
echo intval(array());                 // 0
echo intval(array('foo', 'bar'));     // 1
?>

可以按如下方式更改数据类型

$number = "1.234";

echo gettype ($number) . "\n"; //Returns string

settype($number , "float");

echo gettype ($number) . "\n"; //Returns float

由于历史原因,浮动时返回“double”。

PHP文档

派对迟到了,但这里有另一种方法:

function cast_to_number($input) {
    if(is_float($input) || is_int($input)) {
        return $input;
    }
    if(!is_string($input)) {
        return false;
    }
    if(preg_match('/^-?\d+$/', $input)) {
        return intval($input);
    }
    if(preg_match('/^-?\d+\.\d+$/', $input)) {
        return floatval($input);
    }
    return false;
}

cast_to_number('123.45');       // (float) 123.45
cast_to_number('-123.45');      // (float) -123.45
cast_to_number('123');          // (int) 123
cast_to_number('-123');         // (int) -123
cast_to_number('foo 123 bar');  // false