PHP 中输​​出的默认小数位数

Default Number of Decimal Places to Output in PHP(PHP 中输​​出的默认小数位数)
本文介绍了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 中输​​出的默认小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)