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

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

问题描述

我在家里的开发箱上做我的php工作,在那里我有一个基本的LAMP设置.当我在首页上查看自己的网站时,我回显的任何数字都会被自动截断至最低要求的精度.例如,将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.

Muchas gracias.

Muchas gracias.

推荐答案

当您不能依赖PHP配置时,请不要忘记

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天全站免登陆