number_format() Function Use in PHP 8.4 and PHP 8.5

In PHP 8.5, the number_format() function behaves exactly the same as it did in PHP 8.4. There are no new changes, deprecations, or behavioral updates specific to this function in either version.

The function is used to format a number with grouped thousands (commas), and allows you to set the number of decimal places and customize the separators.

Function Signature

number_format(
   float $num,
   int $decimals = 0,
   ?string $decimal_separator = ".",
   ?string $thousands_separator = ","
): string

Example (Works the same in PHP 8.4 and 8.5).

<?php
$number = 1234567.891;

// 1. Default: No decimals, with commas
echo number_format($number); 
// Output: 1,234,568 (Rounded)

// 2. With 2 decimal places
echo number_format($number, 2); 
// Output: 1,234,567.89

// 3. Custom Separators (e.g., European style)
// Using comma for decimals and a dot for thousands
echo number_format($number, 2, , , . ); 
// Output: 1.234.567,89

// 4. Without a thousands separator
echo number_format($number, 2, . , ); 
// Output: 1234567.89
?>

15 Views