PHP 中输​​出的默认小数位数 [英] Default Number of Decimal Places to Output in PHP

查看:25
本文介绍了PHP 中输​​出的默认小数位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在家里的开发盒上做我的 php 工作,在那里我有一个基本的 LAMP 设置.当我在我的 home box 上查看我的网站时,我回显的任何数字都会自动截断到最低要求的精度.比如2回显为2,2.2000回显为2.2.

I do my php work on my dev box at home, where I've got a rudimentary LAMP setup. When I look at my website on my home box, any numbers I echo are automatically truncated to the least required precision. Eg 2 is echoed as 2, 2.2000 is echoed as 2.2.

在生产框上,所有数字都与至少一个不必要的零相呼应,例如,100 变为 100.0.在这两个盒子上,PHP 版本都是 5.2.5.有没有人知道我可以更改的设置会强制 PHP 自动删除任何不必要的零?我不想去我的代码中输出数字并用 printf 或类似的东西替换 echo 的每个地方.

On the production box, all the numbers are echoed with at least one unnecessary zero, eg 100 becomes 100.0. On both boxes, the PHP version is 5.2.5. Does anyone know offhand if there is a setting I can change which will force PHP to automatically remove any unnecessary zeroes? I don't want to have to go to every place in my code where I output a number and replace echo with printf or something like that.

非常感谢.

推荐答案

当你不能依赖 PHP 配置时,不要忘记 number_format() 可用于定义如何返回数字,例如:

And when you can't rely on the PHP configuration, don't forget about number_format() which you can use to define how a number is returned, ex:

// displays 3.14 as 3 and 4.00 as 4    
print number_format($price, 0); 
// display 4 as 4.00 and 1234.56 as 1,234.56 aka money style
print number_format($int, 2, ".", ","); 

PS:并尽量避免使用 money_format(),因为它在 Windows 和其他一些机器上不起作用

PS: and try to avoid using money_format(), as it won't work on Windows and some other boxes

这篇关于PHP 中输​​出的默认小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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