表示从MySQL数据库中获取“money”属性时在PHP中使用的正确格式是什么?
I'm using the "number_format" function to denote money" attribute in PHP/MySQL.
The attribute itself is stored in my database.
Select account_balance from my_table where login = 'xxxxxx';
$correct_account_balance = number_format($account_balance,
['my_balance'],2); }
In other words : the denotation "2" will add two extra numbers after the decimal point, as follows : 10.00 (for example)
This code works fine............except for one small problem : if the amount after the decimal point has a zero at the end, it does not display!
For example : if the amount is, say, 10 dollars and 35 cents, it displays correctly : 10.35
However, if the amount is 10 dollars, and 30 cents, it displays as : 10.3 (instead of : 10.30 )
The reason is : my program also performs arithmetical operations on the account_balance AFTER I have converted it using the "number_format" function.
For example :
$correct_account_balance -= 0.25 (this will subtract 0.25 each time the program is executed)
This is why, anytime there is a "zero" at the end of the actual amount (like : 10.30), it displays as : 10.3
Is there anyway to get around this? Google doesn't seem to know;
我在PHP中使用“number_format”函数表示 money strong>“属性 MySQL。 p>
属性本身存储在我的数据库中。 p>
换句话说:表示“ 2 strong >“将在小数点后添加两个额外的数字,如下所示: 10.00 strong>(例如) p>
此代码工作正常........ ....除了一个小问题:如果小数点后面的数量在结尾处为零,则不显示! p>
例如:如果金额是,比方说, 10美元和35美分 strong>,它显示正确: 10.35 strong> p>
但是,如果金额是10美元,30美分,它 显示为: 10.3 strong>(而不是: 10.30 strong>) p>
\ n 原因是:我的程序在使用“number_format”函数转换后对account_balance也执行算术运算。 p>
例如: p>
\ n 这就是为什么,任何时候都有“ 零 strong>“在实际金额结束时(如:10.30),显示为: 10.3 strong> p>
无论如何都要获得 在这附近? 谷歌似乎不知道; p>
div>
从my_table中选择account_balance,其中login ='xxxxxx';
$ correct_account_balance = number_format($ account_balance,
['my_balance'],2);}
code> pre>
$ correct_account_balance - = 0.25(每次执行程序时将减去0.25)
code> pre>
The reason is : my program also performs arithmetical operations on the account_balance AFTER I have converted it using the "number_format" function.
You'll need to re-run number_format
after doing the operations on it.
You really shouldn't run it at all until it's ready for display, either, the commas it adds to larger numbers will hugely mess up your calculations. As an example, the following:
<?php
$number = 100000.30;
$number = number_format($number, 2);
$number -= 0.25;
echo number_format($number, 2);
results in the output:
99.75
Which means you've just stolen $99,900.55 from your customers with a type conversion error.