PHP number_format() Fungsi
Contoh
Format angka:
<?php
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>
Definisi dan Penggunaan
Fungsi number_format() memformat angka dengan ribuan yang dikelompokkan.
Catatan: Fungsi ini mendukung satu, dua, atau empat parameter (bukan tiga).
Sintaksis
number_format(number,decimals,decimalpoint,separator)
Nilai Parameter
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 |
Detail Teknis
Nilai Kembali: | Mengembalikan nomor yang diformat |
---|---|
Versi PHP: | 4+ |
Catatan perubahan: | Pada PHP 5.4, fungsi ini mendukung banyak byte dalam parameter titik desimal dan pemisah . Hanya byte pertama dari setiap pemisah yang digunakan dalam versi yang lebih lama. |
Lebih Banyak Contoh
Contoh
Anda ingin mengembalikan harga: Satu parameter akan membulatkan angka (akan diformat tanpa desimal). Dua parameter akan memberikan hasil yang Anda inginkan:
<?php
$num = 1999.9;
$formattedNum = number_format($num)."<br>";
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
?>
Referensi String PHP