PHP number formatting for decimal and thousand places

Format a number for display

number_format() returns a formatted string. Use it for presentation, not as the stored numeric value used in later calculations.

$amount = 1234567.5;
echo number_format($amount, 2); // 1,234,567.50

number_format() returns a string for display. Keep the original numeric value for calculations and format it only when presenting the result.

<?Php
$number=2512589.66785;
echo "Number = $number ";
echo "<br>With formatting = ".number_format($number); //  2,512,590

// Three decimal place 
echo "<br>With formatting = ".number_format($number,3); // 2,512,589.668 

// Three decimal place, '.' as decimal separator and # as thousand separator 
echo "<br>With formatting = ".number_format($number,3,'.','#'); //2#512#589.668 

// Two decimal place, ',' as decimal separator and : as thousand separator 
echo "<br>With formatting = ".number_format($number,2,',',':'); //2:512:589,67

// Two decimal place, '.' as decimal separator and , as thousand separator 
echo "<br>With formatting = ".number_format($number,2,'.',','); //2,512,589.67
?>

Syntax

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

The function rounds while formatting and returns a string.

Use round() when you need a rounded numeric value. Use number_format() when you need presentation formatting such as decimal and thousands separators.

or we can format the number with specifying whether to display the digits with decimal or without decimal or number of digits to display after the decimal. We can place comma ( ,) after a group of thousands. We can use other symbols in place of comma (,) for thousands and we also can replace (.) dot with symbol of our choice to be used as decimal.
Here are some examples for number formatting.

Negative decimals in PHP 8.3+

From PHP 8.3, a negative $decimals value rounds digits before the decimal point.

echo number_format(1234.5678, -2); // 1,200

Example: Currency-style display

$amount = 1234567.89;
echo number_format($amount, 2, '.', ',') . ' USD';  // Output: 1,234,567.89 USD

Example: Formatting Negative Numbers

$number = -123456.789;
echo number_format($number, 2, '.', ',');  // Output: -123,456.79

Math Functions Rounding off value pow() to get exponential value


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer