kennedy32
V2EX  ›  PHP

关于四舍五入

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