SQL PHP HTML ASP JavaScript articles and free scripts to download
 

PHP number formatting for decimal and thousand places

PHP has powerfull math functions. Formatting a number is required while displaying or preparing for some other processing. One common requirement is displaying values after a division with fractions. One way is to round off the number 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. Using PHP number_format function we can do all these formatting. Here are some examples for number formatting.


$number=2512589.66785;

echo "Number = $number ";

echo " The value after formating = ".number_format($number);
// The above line will display 2,512,590 ( without decimal and rounded)

echo "<br> The value after formating = ".number_format($number,3);
// Will display 2,512,589.668 ( with , after each thousand and 3 digits after decimal
// rounded )

echo "<br> The value after formating = ".number_format($number,3,"#",":");
// Will display 2:512:589#668 ( with : after each thousand and # as decimal symbol )


Visit the links below to see other php math functions.

PHP Math Functions
PHP Math Functions  |Addition Subtraction  |Maximum and Minimum numbers  |PHP Round  | Random number generator  | NUmber Formatting  | Ceil and floor value of a number  | Modulus or reminder

Discuss this tutorial at forum


 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorials
Math Functions
PHP Sections
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.