• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Web Development / PHP / PHP Strings / Formatting Numbers with PHP’s number_format() Function

Formatting Numbers with PHP’s number_format() Function

29 January 2010 / Leave a Comment

When displaying a number in a Web page, it’s nice to format the number in an easy-to-read way. For example, 123,456.78 is nicer to read than 123456.784321.

PHP’s number_format() function gives you an easy way to format numbers for displaying to the user. You can separate thousands with commas or other separators, choose the decimal point character, and choose the number of decimal places to display (or display no decimals at all).

Basic number formatting

In its most basic form, number_format() accepts a number to format, and returns a string containing the formatted number, rounded to the nearest whole number, with commas between each group of thousands:

$myNumber = 1234567.89;

// Displays "1,234,568"
echo number_format( $myNumber );
Since number_format() returns a string containing separator characters, you can’t use the output of number_format() as a numeric value with other math operators or functions. Therefore, make sure you do all your calculations on the original numeric value, and only convert the value to a string with number_format() when you’re ready to display it to the user.

Specifying the number of decimal places

To add decimal places to the formatted number, specify the number of decimals to add as the second argument. number_format() rounds the number up or down to the specified number of decimals:

$myNumber = 123456.784321;

// Displays "123,456.78"
echo number_format( $myNumber, 2 );

// Displays "123,456.8"
echo number_format( $myNumber, 1 );

Using different separators

By default, number_format() uses a dot for the decimal point, and a comma for the thousands separator. To use different separators, pass the new decimal point separator as the third argument, and the new thousands separator as the fourth argument.

The following example displays a number using the French convention — a comma for the decimal point, and a space for the thousands separator:

$myNumber = 123456.784321;

// Displays "123 456,78"
echo number_format( $myNumber, 2, ',', ' ' );

If you wanted to display a number in English notation rounded to 2 decimal places, but without thousands separators, you could write the following:

$myNumber = 123456.784321;

// Displays "123456.78"
echo number_format( $myNumber, 2, '.', '' );
You can use PHP’s localeconv() function to find out the computer’s current locale settings for the decimal point and thousands separator — handy for plugging into number_format().

Summary

number_format() gives you a simple way to display numbers in a nice, easy-to-read format. You’ve learned how to call number_format(), how to specify the number of decimals to display, and how to display numbers using a different thousands separator and decimal point.

Incidentally, if you need to display a number using words — for example, “one thousand two hundred and thirty-four” — check out the Numbers_Words PEAR module.

Happy coding!

Filed Under: PHP Strings Tagged With: decimal places, decimal point, decimals, locale, number_format, php, thousands separator

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

To include a block of code in your comment, surround it with <pre> ... </pre> tags. You can include smaller code snippets inside some normal text by surrounding them with <code> ... </code> tags.

Allowed tags in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre> .

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2023 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits