在数字的小数点前加零 [英] Add zero to before decimal point of a number
本文介绍了在数字的小数点前加零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们如何在PHP的小数点前添加零?
How do we we add a zero in front of the decimal place in PHP?
我想将 .96
转换为 0.96
现在在我的php代码中,我正在通过使用wordpress get_attribute
Now in my php code, I'm fetching data by using wordpress get_attribute
<td><?php echo $_product->get_attribute('pa_carat');?></td>
推荐答案
The function you want is number_format
echo number_format(0.96, 2);
或者,如果缺少它,您可以检查并添加它.
Alternatively you can just check for and prepend it if it is missing.
if ($num[0] == '.')
$num = '0' . $num;
尽管这不会处理负数,但您需要为此编写更多的逻辑.
This wont handle negative numbers though, you would need to write a little more logic for that.
这篇关于在数字的小数点前加零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文