PHP - number_format
Trieda
Metóda - number_format
(PHP 4, PHP 5, PHP 7)
The function formats a number according to given parameters. The function supports 1, 2, or 4 parameters entered (not 3).
Procedurálne
- function number_format (float $number, int $decimals = 0, string $dec_point = ., string $thousands_sep = ,) : string
- function number_format (float $number, int $decimals = 0, string $dec_point = ., string $thousands_sep = ,) : string
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| $number | float | The number we want to be formatted. | |
| $decimals | int | 0 | The parameter specifies the amount of decimal places. |
| $dec_point | string | . | Specifies the string separator we want to use for the decimal point. If we want to set this parameter, we also have to set the last parameter. |
| $thousands_sep | string | , | The string we want to use as the thousands separator. |
Mávratovej hodnoty
Vracia: string
The function returns the formatted number.
Príklady
In the first example we set different parameters to compare the outputs.
<?php
$number = 250000.2557;
// We do not set any optional parameter.
echo number_format($number) . "<br>";
// We can set only the amount of decimal places.
echo number_format($number, 2) . "<br>";
// All the parameters are set.
echo number_format($number, 3, ",", ".") . "<br>";
// The number can also be passed as string.
echo number_format("250000.2557", 1, ",", ".") . "<br>";
In the second example we set the parameters according to the number format used in most countries of the continental Europe.
<?php
echo number_format("1250000.2557", 2, ",", " ") . "<br>";
Formatting of the telephone numbers is really easy due to this function.
<?php
echo number_format("777555123",0 , "", "-") . "<br>";
echo number_format("777555123",0 , "", " ") . "<br>";
Súvisiace manuály
- function printf (string $format, mixed $args, mixed $...) : int
