money_format() options

☆樱花仙子☆ 提交于 2019-12-09 05:52:49

问题


I am looking at the money_format function in php and confused on how to get it to format the way I want. I do not want USD in front of my string, I want a comma every 3 digits and 2 decimal points so 12345.67 will be formated to $12,345.67

Thanks.


回答1:


Have you looked at number_format? It's a little easier I think.

print number_format( 1234567, 2, ",", "." ); for example.




回答2:


To answer the original question using money_format, it's simply

money_format('%.2n', 12345.67);

money_format always starts with a %. The .2 indicates you want to always use 2 decimal places. If you pass a whole number it will add .00 to the end. The n indicates you want use the locale's national currency format.

It's important to note that you must have the locale set correctly since money_format is based on the locale you have set for LC_MONETARY. To set the locale use setlocale. In this case it appears you want en_US.

setlocale(LC_MONETARY, 'en_US.UTF-8');


来源:https://stackoverflow.com/questions/2952112/money-format-options

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!