Categories: PHP

number_format – PHP String Functions

Syntax :

number_format ( number, decimals, decimal_point, thousands_sep);

Description :

number_format() function helps in formating a number (Floating number) with grouped thousands.

Note : This function takes either one, two, or four parameters (not three).

Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|lef|lef|left”]
Name, Required /Optional, Value Type, Description

number, Required, Float , It is the input number.

decimals, Optional, Integer , It specifies the number for the decimal point.

decimal_point, Optional, String , It specifies the separator for decimal point.

thousands_sep, Optional, String , It specifies the separator for thousands.

[/table]


Output :

It returns the formatted number.


ChangeLog :

[table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
Version, Description
PHP 7.2.0, number_format() has been changed not to return -0. However in earlier version -0 could be returned for cases like where number would be -0.05.
PHP 5.4.0 , It supports multiple bytes in dec_point and thousands_sep now. Only the first byte of each separator was used in older versions..
[/table]


number_format() – PHP Functions Example 1 :
<?php
$number=999999;
echo number_format($number).'<br>';
echo number_format($number, 2).'<br>';
echo number_format($number, 3).'<br>';
echo number_format($number, 2, ',', '.');
?>

Output of above code in the browser is as below:

999,999
999,999.00
999,999.000
999.999,00

number_format() – PHP Functions Example 2 :
<?php
$number=999999.99;
echo number_format($number).'<br>'; //One parameter will round the number. 
echo number_format($number, 2).'<br>';
echo number_format($number, 3).'<br>';
echo number_format($number, 2, ',', '.').'<br>';
echo number_format($number, 2, ',', 'x').'<br>';
?>

Output of above code in the browser is as below:

1,000,000
999,999.99
999,999.990
999.999,99
999×999,99

jyoti rani

Recent Posts

What Is a Progressive Web App? Why Would You Need One?

App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…

11 months ago

7 Most Popular Paid Online Advertising Strategy

As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…

1 year ago

The Importance of User-Centered Design in Mobile App Development

Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…

1 year ago

Healthcare Mobile App Development: A Complete Guide for Founders

COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…

1 year ago

Exploring Diverse WordPress Theme Niches: A Comprehensive Guide

Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…

1 year ago

8 Awesome Blog Content Ideas for Movers to Skyrocket the SEO

For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…

1 year ago