kennedy32
V2EX  ›  PHP

关于四舍五入

  •  
  •   kennedy32 · Jun 12, 2013 · 3684 views
    This topic created in 4762 days ago, the information mentioned may be changed or developed.
    <?php
    $a=56;
    $b=12;
    $c=$a/$b;
    echo $c;
    ?>

    输出4.6666666666667

    <?php
    $a=56;
    $b=12;
    $c=(int)($a/$b);
    echo $c;
    ?>

    输出4

    所以不是四舍五入?
    10 replies    1970-01-01 08:00:00 +08:00
    LincolnDz
        1
    LincolnDz  
       Jun 12, 2013
    int是强制整形,只取整数部分
    怎么变成四舍五入了....
    四舍五入要用round()
    float round ( float val [, int precision])
    qiayue
        2
    qiayue  
    PRO
       Jun 12, 2013
    我发现楼主问的问题在php手册里边都可以找到,建议多翻翻手册
    kennedy32
        3
    kennedy32  
    OP
       Jun 12, 2013
    @qiayue 有中文版手册么
    raincious
        4
    raincious  
       Jun 12, 2013
    echo intval(1.455555); // 1
    echo round(1.55555, 0); // 2
    echo ceil(1.11111); // 2
    itommy
        5
    itommy  
       Jun 12, 2013   ❤️ 1
    kennedy32
        6
    kennedy32  
    OP
       Jun 12, 2013
    @itommy tks
    heliumhgy
        7
    heliumhgy  
       Jun 12, 2013
    有些语言的 round 不一定是四舍五入吧,http://en.wikipedia.org/wiki/Rounding#Tie-breaking
    曾经遇到过奇葩的四舍六入。。。
    kennedy32
        8
    kennedy32  
    OP
       Jun 12, 2013
    @itommy 有没有那种持续更新的中文电子书?
    DaniloSam
        9
    DaniloSam  
       Jun 12, 2013
    思想家要学会看文档啊
    itommy
        10
    itommy  
       Jun 12, 2013
    @kennedy32 这php手册不需要持续更新的吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2819 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 12:59 · PVG 20:59 · LAX 05:59 · JFK 08:59
    ♥ Do have faith in what you're doing.