我想将这些类型的值“3”、“2.34”、“0.234343”等转换为数字。在JavaScript中,我们可以使用Number(),但在PHP中有没有类似的方法?
Input Output
'2' 2
'2.34' 2.34
'0.3454545' 0.3454545
我想将这些类型的值“3”、“2.34”、“0.234343”等转换为数字。在JavaScript中,我们可以使用Number(),但在PHP中有没有类似的方法?
Input Output
'2' 2
'2.34' 2.34
'0.3454545' 0.3454545
当前回答
可以按如下方式更改数据类型
$number = "1.234";
echo gettype ($number) . "\n"; //Returns string
settype($number , "float");
echo gettype ($number) . "\n"; //Returns float
由于历史原因,浮动时返回“double”。
PHP文档
其他回答
PHP将在限制范围内为您提供
<?php
$str = "3.148";
$num = $str;
printf("%f\n", $num);
?>
好的,所以我刚刚遇到了这个问题。我的问题是所讨论的数字/字符串具有不同的位数。有些没有小数,有些有几个。所以对我来说,使用int、float、double、intval或floatval都会根据数字给出不同的结果。
所以,简单的解决方案。。。将字符串除以服务器端的1。这会将其强制为一个数字,并保留所有数字,同时修剪不必要的0。它不漂亮,但它有效。
"your number string" / 1
Input Output
"17" 17
"84.874" 84.874
".00234" .00234
".123000" .123
"032" 32
如果你想要一个字符串的数值,但因为不确定而不想将其转换为float/int,这个技巧会将其转换成正确的类型:
function get_numeric($val) {
if (is_numeric($val)) {
return $val + 0;
}
return 0;
}
Example:
<?php
get_numeric('3'); // int(3)
get_numeric('1.2'); // float(1.2)
get_numeric('3.0'); // float(3)
?>
资料来源:https://www.php.net/manual/en/function.is-numeric.php#107326
下面是我为自己编写的一个函数:
它还返回布尔、整数、双精度和实数的简写版本。
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';
如果你事先不知道你有一个浮点数还是一个整数,如果字符串可能包含特殊字符(如空格、欧元等),并且如果它可以包含多于1个点或逗号,您可以使用此功能:
// This function strip spaces and other characters from a string and return a number.
// It works for integer and float.
// It expect decimal delimiter to be either a '.' or ','
// Note: everything after an eventual 2nd decimal delimiter will be removed.
function stringToNumber($string) {
// return 0 if the string contains no number at all or is not a string:
if (!is_string($string) || !preg_match('/\d/', $string)) {
return 0;
}
// Replace all ',' with '.':
$workingString = str_replace(',', '.', $string);
// Keep only number and '.':
$workingString = preg_replace("/[^0-9.]+/", "", $workingString);
// Split the integer part and the decimal part,
// (and eventually a third part if there are more
// than 1 decimal delimiter in the string):
$explodedString = explode('.', $workingString, 3);
if ($explodedString[0] === '') {
// No number was present before the first decimal delimiter,
// so we assume it was meant to be a 0:
$explodedString[0] = '0';
}
if (sizeof($explodedString) === 1) {
// No decimal delimiter was present in the string,
// create a string representing an integer:
$workingString = $explodedString[0];
} else {
// A decimal delimiter was present,
// create a string representing a float:
$workingString = $explodedString[0] . '.' . $explodedString[1];
}
// Create a number from this now non-ambiguous string:
$number = $workingString * 1;
return $number;
}