PHP number_format()関数
例
フォーマット番号:
<?php
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>
定義と使用法
number_format()関数は、数千をグループ化して数値をフォーマットします。
注:この関数は、1つ、2つ、または4つのパラメーター(3つではない)をサポートします。
構文
number_format(number,decimals,decimalpoint,separator)
パラメータ値
Parameter | Description |
---|---|
number | Required. The number to be formatted. If no other parameters are set, the number will be formatted without decimals and with comma (,) as the thousands separator. |
decimals | Optional. Specifies how many decimals. If this parameter is set, the number will be formatted with a dot (.) as decimal point |
decimalpoint | Optional. Specifies what string to use for decimal point |
separator | Optional. Specifies what string to use for thousands separator. Only the first character of separator is used. For example, "xxx" will give the same output as "x" Note: If this parameter is given, all other parameters are required as well |
技術的な詳細
戻り値: | フォーマットされた数値を返します |
---|---|
PHPバージョン: | 4歳以上 |
変更ログ: | PHP 5.4以降、この関数はパラメーター decimalpointおよびseparatorで複数バイトをサポートします。古いバージョンでは、各セパレータの最初のバイトのみが使用されていました。 |
その他の例
例
価格を返したい場合:1つのパラメーターで数値が丸められます(小数点なしでフォーマットされます)。2つのパラメータで希望の結果が得られます。
<?php
$num = 1999.9;
$formattedNum = number_format($num)."<br>";
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
?>
❮PHP文字列リファレンス