PHP货币格式在需要时添加小数和逗号 [英] PHP money format add decimal and comma when needed
本文介绍了PHP货币格式在需要时添加小数和逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用php将这个数字 247936 转换为 $ 2,479.36 ?
How do I turn this number 247936 into this $2,479.36 using php?
我尝试了几种方法,例如
I tried several options such as:
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', '247936'); // OUTPUT: USD 247,936.00
我也尝试过此操作
echo number_format(247936, 2); // echos '247,936.00'
我什至尝试过,但效果不佳
I even tried this but it didn't work as well
echo number_format(247936, 2,'.', ','); // echos 247,936.00
推荐答案
似乎您的变量位于美分,因此除以100
It looks like your variable is in cents, so divide it by 100
echo number_format((247936 / 100), 2, '.', ',');
输出为: 2,479.36
这篇关于PHP货币格式在需要时添加小数和逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文