代码之家  ›  专栏  ›  技术社区  ›  rob.m

如何实现货币价值的四舍五入?

php
  •  0
  • rob.m  · 技术社区  · 7 年前

    我愿意:

    echo "Internal studio total costs = ".$sumInternalStudio."<br>";
    

    我得到: Internal studio total costs = 3847401

    如果我这样做了

    $sumInternalStudio = money_format('%.2n', $sumInternalStudio);
    echo "Internal studio total costs = ".$sumInternalStudio."<br>"
    

    我明白了

    Internal studio total costs = € 3.847.401,00
    

    但我想得到 € 4

    如果我这样做:

    $sumInternalStudio = round($sumInternalStudio);
    $sumInternalStudio = money_format('%.2n', $sumInternalStudio);
    echo "Internal studio total costs = ".$sumInternalStudio."<br>";
    

    我明白了

    Internal studio total costs = € 3.847.401,0
    

    如果我这么做了

    $sumInternalStudio = money_format('%.2n', $sumInternalStudio);
    $sumInternalStudio = round($sumInternalStudio);
    echo "Internal studio total costs = ".$sumInternalStudio."<br>";
    

    我明白了 Internal studio total costs = 0

    如何四舍五入到最近的最高值?

    使现代化

    我试过了

    $sumInternalStudio = number_format(($sumInternalStudio/100),2); 
    $sumInternalStudio = round($sumInternalStudio);
    $sumInternalStudio = money_format('%.2n', $sumInternalStudio);
    echo "Internal studio total costs = ".$sumInternalStudio."<br>";
    

    我得到了 Internal studio total costs = € 38,00 但应该如此 € 4

    0 回复  |  直到 7 年前
        1
  •  0
  •   Minding Patec    7 年前

    使用 number_format :

    $formattedSum = '€ '.number_format(3847401, 2, ',', '.');
    

    € 3.84

    round :

    $formattedSum = '€ '.round(3847401 / 1000000);
    

    4.

    推荐文章