dongwalun2507 2008-12-10 18:35
浏览 182
已采纳

PHP中输出的默认小数位数

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.

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.

  • 写回答

6条回答 默认 最新

  • douchigu1723 2008-12-10 18:51
    关注

    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: and try to avoid using money_format(), as it won't work on Windows and some other boxes

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)