PHPの関数 number_formatについて
PHPでnumber_formatという関数を扱ったので使い方等を記載いたします。
カンマ区切り
4桁以上の大きい数字を表示するときはカンマ区切りにした方が見やすいのは当然だと思います。 そこでnumber_formatを使うとカンマ区切りで表示してくれます。
$money1 = 100000; $money2 = 200000; echo $money1; echo "\n"; echo number_format($money2);
出力結果
100000 200,000
小数点以下の扱い
number_formatは小数点以下を四捨五入してくれます。
$money3 = 1000.3; $money4 = 2000.6; echo number_format($money3); echo "\n"; echo number_format($money4);
出力結果
1,000 2,001
ただ小数点第何位で四捨五入するかを指定したいときは第二引数に記入しましょう。
$money5 = 1000.373; $money6 = 2000.6438; echo number_format($money5,2); echo "\n"; echo number_format($money6,3);
出力結果
1,000.37 2,000.644
といった形になります。
以下のサイトを参考にいたしました。