我想把这些值'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
如果您想获取字符串的数值,并且不想将其转换为浮点数/整数,因为您不确定,那么这个技巧将把它转换为正确的类型:
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/zh/function.is-numeric.php#107326
get_numeric(010); // int 8
get_numeric('010'); // int 10
- vralle对于一些情况下可能有用并更加安全的答案,这里有个小提示。
您可能希望先检查字符串是否实际包含有效的数字值,然后再将其转换为数字类型(例如,如果您必须处理来自将整数转换为字符串的数据库的数据)。 您可以使用 is_numeric()
然后使用 floatval()
:
$a = "whatever"; // any variable
if (is_numeric($a))
var_dump(floatval($a)); // type is float
else
var_dump($a); // any type
intval("4C775916") => 4
可能会导致一个严重的错误。 - Daryl以下是能够实现您所需功能的函数。首先,我们检查该值是否可以被理解为数字,如果可以,我们将其转换为整数和浮点数。如果整数和浮点数相同(例如,5 == 5.0),则返回整数值。如果整数和浮点数不相同(例如,5 != 5.3),则假定您需要浮点数的精度并返回该值。如果该值不是数字,则会发出警告并返回null。
function toNumber($val) {
if (is_numeric($val)) {
$int = (int)$val;
$float = (float)$val;
$val = ($int == $float) ? $int : $float;
return $val;
} else {
trigger_error("Cannot cast $val to a number", E_USER_WARNING);
return null;
}
}
我已经阅读了一些答案,没有人提到 PHP 数字转换中最大的陷阱。
得票最高的答案建议执行以下操作:
$str = "3.14"
$intstr = (int)$str // now it's a number equal to 3
太棒了。PHP可以进行直接类型转换。但是如果我们采取以下做法呢?
$str = "3.14is_trash"
$intstr = (int)$str
PHP是否认为这种转换有效?
显然是的。
PHP会读取字符串,直到找到所需类型的第一个非数字字符。对于整数来说,数字字符是[0-9]。因此,它读取了3
,因为它在[0-9]字符范围内,继续阅读。读取 .
并在那里停止,因为它不在[0-9]范围内。
如果您将其强制转换为浮点数或双精度浮点数,情况也是一样的。PHP会读取3
,然后 .
,然后1
,然后4
,并在那里停止,因为它不是有效的浮点数字符。
因此,"million" >= 1000000
计算结果为false,但"1000000million" >= 1000000
计算结果为true
。
另请参阅:
https://www.php.net/manual/en/language.operators.comparison.php 比较时如何进行转换
https://www.php.net/manual/en/language.types.string.php#language.types.string.conversion 如何将字符串转换为相应的数字
is_numeric($str)
可以帮助解决这个问题。(正如之前两个答案中所示 - 所以说“没有人提到最大的警告”并不完全准确,尽管我没有看到有人详细解释这个问题。) - ToolmakerSteve我遇到了这个问题。我的问题是有些数字/字符串的位数不同。有些没有小数点,而有些则有几个。因此,对于我来说,使用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
除了Boykodev的回答,我建议这样做:
Input Output
'2' * 1 2 (int)
'2.34' * 1 2.34 (float)
'0.3454545' * 1 0.3454545 (float)
$id_cron = (string)date('YmdHi'); $target_id_cron = $id_cron - 1;
而不是 $target_id_cron = (int)$id_cron - 1;
。 - inMILD$num = '12,24' * 1
时,出现了错误 A non well formed numeric value encountered
。有什么建议吗? - Agnes PalitPHP
是以英语为中心的。只承认 .
作为小数分隔符。尝试使用 '12.24' * 1
。虽然我个人更喜欢使用 +0
。加法而不是乘法。 - ToolmakerSteve将数字乘以1,使字符串转换为数字类型。
//String value
$string = "5.1"
if(is_numeric($string)){
$numeric_string = $string*1;
}
这是我为自己编写的一个函数,以简化事情:
它还返回布尔值、整数、双精度和实数的简写版本。
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()函数会在检查类型之前将数字字符串转换为数字。
因此:
type("5", true) 将返回 int
type("3.7", true) 将返回 float
type("500") 将返回 string
但请小心,由于这是一种虚假的检查方法,您实际的变量仍然是一个字符串。如果需要,您需要将实际变量转换为正确的类型。我只需要用它来检查数据库是否应该加载项目ID或别名,因此在运行时它将被解析为字符串,不会产生任何意外效果。
编辑
如果您想检测对象是否是函数,请将此情况添加到switch中:
case 'object': return is_callable($mixed)?'function':'object';
$var = +$str;
-身份运算符。 - ToolmakerSteve
int
或float
(当给定int
时,通常不需要float
)。 - Matthieu Napoli2
被内部存储为Number:"JavaScript代码中的数字字面量(如37)是浮点值,而不是整数。在日常使用中没有单独的整数类型。(JavaScript现在有一个BigInt类型,但它并不是为了取代Number而设计的。37仍然是一个Number,而不是BigInt。)"因此,在JS中,Number(your_string)
之所以能够实现你想要的功能,是因为其他地方的逻辑默认将2
显示为"2"
而不是"2.0"
。 - ToolmakerSteve