您可以关注link 了解更多关于如何在 PHP 中将字符串/数字转换为数字/浮点数/十进制。
这就是这个链接所说的......
方法一:使用 number_format() 函数。 number_format() 函数用于将字符串转换为数字。它在成功时返回格式化的数字,否则在失败时给出 E_WARNING。
$num = "1000.314";
//Convert string in number using
//number_format(), function
echo number_format($num), "\n";
//Convert string in number using
//number_format(), function
echo number_format($num, 2);
方法2:使用类型转换:类型转换可以直接将字符串转换为浮点、双精度或整数原始类型。这是将字符串转换为数字的最佳方法,无需任何函数。
// Number in string format
$num = "1000.314";
// Type cast using int
echo (int)$num, "\n";
// Type cast using float
echo (float)$num, "\n";
// Type cast using double
echo (double)$num;
方法 3:使用 intval() 和 floatval() 函数。 intval() 和 floatval() 函数也可用于将字符串分别转换为其对应的整数和浮点值。
// Number in string format
$num = "1000.314";
// intval() function to convert
// string into integer
echo intval($num), "\n";
// floatval() function to convert
// string to float
echo floatval($num);
方法4:通过加0或进行数学运算。字符串数字也可以通过在字符串中添加 0 转换为整数或浮点数。在 PHP 中,执行数学运算时,字符串被隐式转换为整数或浮点数。
// Number into string format
$num = "1000.314";
// Performing mathematical operation
// to implicitly type conversion
echo $num + 0, "\n";
// Performing mathematical operation
// to implicitly type conversion
echo $num + 0.0, "\n";
// Performing mathematical operation
// to implicitly type conversion
echo $num + 0.1;