Default Number of Decimal Places to Output in 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 中输出的默认小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 中输出的默认小数位数
基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
