from.. 2010-12-19 15:10 采纳率: 100%
浏览 999
已采纳

显示数到小数点后2位

What's the correct way to round a PHP string to 2 decimal places?

$number = "520"; // It's a string from a DB

$formatted_number = round_to_2dp($number);

echo $formatted_number;

The output should be 520.00;

How should the round_to_2dp() function definition be?

转载于:https://stackoverflow.com/questions/4483540/show-a-number-to-2-decimal-places

  • 写回答

20条回答 默认 最新

  • 旧行李 2010-12-19 15:14
    关注

    You can use number_format():

    return number_format((float)$number, 2, '.', '');
    

    Example:

    $foo = "105";
    echo number_format((float)$foo, 2, '.', '');  // Outputs -> 105.00
    

    This function returns a string.

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料