将百分比转换为小数并删除 PHP 中的百分号?

Convert a percentage into a decimal and remove percent sign in PHP?

在 PHP 中移动两位小数并删除百分号的最佳方法是什么?

例如:

9.5% 变成 .095

有专门的功能吗?

您可以像这样轻松创建自己的函数:

private function myConvertFunction($percentage)
{
    return floatval(str_replace('%', '', $percentage)) /100;
}

会return

echo 'The result is : ' . myConvertFunction('9.5%');

The result is : 0.095

首先,从值中删除 % 符号

$number = str_replace('%','',$number); 

将值转换为所需格式

$number = $number * 0.01;

只要“%”在末尾,您就可以简单地将字符串转换为浮点数并除以 100:

echo (float)'9.5%'/100;

输出:

0.095