number_format 和负数的问题
现在我在数据库中将数字存储为 double(11,3) 。有些数字可能是更大的负数。我现在正在玩的是 -3000.000
当我在 3000.000 上使用 number_format 时,它会返回 3,000 当我在 -3000.000 上使用 number_format 时,它返回 -3
有什么想法为什么会发生这种情况以及我可以采取什么措施来解决它吗?我现在有点不知道为什么会发生这种情况。
谢谢,
杰夫
编辑:我让它与以下代码一起工作:
$number = abs($row['Amount']) * -1; $final = number_format($number,2);
现在为什么会起作用而不是:
$final = number_format($row['Amount'],2);
我没有线索,但至少我找到了一个解决方案,感谢您的帮助:)
Right now I'm storing numbers as a double(11,3) in my database. Some of the numbers can be larger negatives. The one I'm playing with right now is -3000.000
When I use number_format on just 3000.000 it returns 3,000
When I use number_format on -3000.000 it returns -3
Any ideas why this is happening and what I can do to fix it? I'm kind of at a loss right now as to why this is happening.
Thanks,
Jeff
EDIT: I got it to work with the following code:
$number = abs($row['Amount']) * -1; $final = number_format($number,2);
Now why that would work and not:
$final = number_format($row['Amount'],2);
I haven't got a clue, but at least I found a solution, thanks for the help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
try this:
number_format(-3000.000, 2)
的结果是字符串,"-3,000.00"
。如果您不明白,请使用重现您的问题的代码更新问题。
The result from
number_format(-3000.000, 2)
is the string,"-3,000.00"
.If you don't get that, please update the question with code that reproduces your problem.
首先确保您的号码从数据库中提取为 -3000。否则以前的答案和评论都很好。
First make sure your number is extracted as -3000 from database. Otherwise previous answers and comments are good.
根据Shameer的建议,我得出了这个解决方案。
将返回负值的尾随零(可能如预期):
based on Shameer's suggestion, I came to this solution.
trailing zeros on negative values will be returned (probably as expected):