PHP - number_format 和舍入 [英] PHP - number_format and rounding

查看:44
本文介绍了PHP - number_format 和舍入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP 中的number_format 函数似乎默认为四舍五入.我的理解是这个函数对于用逗号分隔每三个数字很有用.就像使用 number_format 时 1403423 变成 1,404,423.

The number_format function in PHP seems to round by default. My understanding is that this function is useful for separating every three numbers with a comma. Like 1403423 becomes 1,404,423 when using number_format.

所以,如果我有一个大数想要四舍五入到两位小数,我该如何做到这一点并且仍然正确显示逗号?

So, if I have a large number that I want rounded to two decimal places, how can I do this and still have the commas properly displayed?

期望的行为:12042.529 --> 12,042.53

Desired behavior: 12042.529 --> 12,042.53

推荐答案

$a=12042.529;
echo number_format($a,2,'.',',');

这篇关于PHP - number_format 和舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆