PHP number: decimal point visible only if needed

后端 未结 12 1585
死守一世寂寞
死守一世寂寞 2021-01-31 06:48

I\'d like to know if exists some function to automatically format a number by it\'s decimal, so if I have:



        
12条回答
  •  迷失自我
    2021-01-31 07:31

    If you are targeting US currency I like to use this method:

    function moneyform($number, $symbol = true) {
        return str_replace(".00", "", money_format(($symbol? '%.2n' : "%!n"), $number));
    }
    
    moneyform(1300999);
    -->$1,300,999
    
    moneyform(2500.99);
    -->$2,500.99
    
    moneyform(2500.99, false);
    -->2,500.99
    

提交回复
热议问题