如何转换数字字符串中的最后两位数字始终为十进制(尽管为 00)


How to convert a string of numbers in a number with the last two digits always decimal (though 00)

我有这样的数字字符串:

- 00986756849
- 007478599700
- 004583930237345

我需要将这些数字字符串转换为具有 PHP 中最后两个十进制值的新数字,即:

- 009867568.49
- 0074785997.00
- 0045839302373.45

如何?

获取字符串值 。 使用 substr_replace 替换子字符串或将子字符串插入到字符串中的特定位置:)

$str = "00986756849";
substr_replace($str,".",strlen($str)-2,0);
$intNumber = (int) $stringNumber;
$decimalNumber = sprintf('%02d', $intNumber);

你可以试试这个

//$str = "10";
$str = "00986756849";
$str_length = strlen($str);
if($str_length>2)
{
    $output = substr_replace($str,".",$str_length-2,0);
}
else
{
    $output = substr_replace($str,".00",$str_length,0);
}
echo $output;