dongmin1166 2016-06-24 02:05
浏览 58

如何在PHP中舍入小十进制数

I have this number:

0.108504578471184

And I would like to round it keeping two decimals, I've tried using the php function round() like this:

round(0.108504578471184, 2)

Expected result:

0.11

Actual Result:

0

Why is it giving me 0 when I've specified that I want 2 decimals? EDIT: As noted in the comments, the problem is something else in the code. I am selecting a xml attribute with this line

$selectedMpoint->xpath('/RFile/Machine/MPoint[@No="'.$MPNo.'"]/Operation[@FrqRange="'.$MPFrqRange.'"]/QParam[@Qid="1"]/@Value')[0];

Echoing the line:

echo $selectedMpoint->xpath('/RFile/Machine/MPoint[@No="'.$MPNo.'"]/Operation[@FrqRange="'.$MPFrqRange.'"]/QParam[@Qid="1"]/@Value')[0];

Gives me this number (same as mentioned before)

0.1085045784711840

But when I try to use round() on it it just gives me 0, I've tried putting it into a variable because I thought that it could be like the [0], 2 at the end messing it up, but i still get 0e

round($selectedMpoint->xpath('/RFile/Machine/MPoint[@No="'.$MPNo.'"]/Operation[@FrqRange="'.$MPFrqRange.'"]/QParam[@Qid="1"]/@Value')[0], 2
  • 写回答

1条回答 默认 最新

  • duanpi2033 2016-06-24 02:16
    关注

    Although round(0.108504578471184, 2) produces 0.11 for me, but you can try using number_format:

    (float) number_format(0.108504578471184, 2);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应