将数字除以正确的小数


Divide the number to a correct decimal

所以我有一个这样的字符串:

000000000050000

最后两个字符应该始终是小数。

所以这里的最终目标是:

500.00

我一直在尝试使用substr来提取最后两个字符,将它们分开,然后通过乘法将它们分成小数,这不起作用,而且看起来非常复杂。

类似这样的东西:

$number = substr($str, 2, -2);
$number/500;

我如何才能达到顶部所示的最终目标。

您可以使用number_format:

$str = "000000000050000";
echo number_format($str / 100, 2, ",", ".");

http://php.net/manual/en/function.number-format.php

您可以使用intval()从字符串中获取数字,并使用number_format()对其进行正确格式化。

$str = "000000000050000";
$int = intval($str, 10); //Base 10, because numbers starting with 0 are automatically considered octal!
$result = number_format($int / 100, 2);