简体   繁体   English

格式化显示的钱:“x” - >“x.00”?

[英]Format money for display: “x” -> “x.00”?

I am storing money in MySQL with float format but .00 are removed since they don't have much meaning but I want to show them to user in a correct money format using PHP. 我用浮动格式在MySQL中存储钱,但是.00被删除,因为它们没有多大意义,但我想用PHP以正确的货币格式向用户显示它们。

I wrote a function which checks whether the money field has any . 我写了一个功能,检查钱区是否有任何. period otherwise add .00 . 否则增加.00

But personally I didn't like this function so I am looking for a built-in function which can do the same job: 但我个人不喜欢这个功能所以我正在寻找一个可以做同样工作的内置函数:

function formatMoney($price){
    if (strpos($price,".") > -1)
        return $price;
    else
        return $price . ".00";
}

Edit: For the people who somehow reached my question should know that money_format doesn't work on Windows Machines as it is explained here below taken from the PHP Manual: 编辑:对于以某种方式达到我的问题的人应该知道money_format在Windows机器上不起作用,因为下面从PHP手册中解释:

The function money_format() is only defined if the system has strfmon capabilities. 仅当系统具有strfmon功能时才定义函数money_format()。 For example, Windows does not, so money_format() is undefined in Windows. 例如,Windows没有,因此在Windows中未定义money_format()。

So you should use number_format in place of it. 所以你应该使用number_format来代替它。 You can see my answer on how I solved this problem. 你可以看到我如何解决这个问题的答案。

Why don't you use money_format ? 你为什么不用money_format

Example: 例:

$price = 43;
money_format('%.2n', $price);

It seems money_format doesn't work on Windows machines then I had to use number_format($price,2) and it worked just perfect. 似乎money_format在Windows机器上不起作用然后我不得不使用number_format($price,2)并且它工作得非常完美。

Note from PHP Manual: PHP手册中的注释:

The function money_format() is only defined if the system has strfmon capabilities. 仅当系统具有strfmon功能时才定义函数money_format() For example, Windows does not, so money_format() is undefined in Windows. 例如,Windows没有,因此在Windows中未定义money_format()。

使用money_formatsprintf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM