如何在 PHP 中在小数点前面添加零?
我想将 .96
转换为 0.96
现在在我的 php 代码中,我使用 wordpress get_attribute
获取数据
<td><?php echo $_product->get_attribute('pa_carat');?></td>
最佳答案
您想要的功能是number_format
echo number_format(0.96, 2);
或者,您可以只检查它,如果丢失则将其添加到前面。
if ($num[0] == '.')
$num = '0' . $num;
不过,这不会处理负数,您需要为此编写更多逻辑。
关于php - 在数字的小数点前添加零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45530636/