PHP Number Formatting Techniques

PHP Number Formatting Techniques

If you are working in PHP, dealing with large numbers, and making it more readable to the users, then “number_format()” is an excellent PHP function that can help you accomplish number formatting tasks.

This function takes a number as input, which may be in integer or floating point values. By default, the number_format function adds a comma(,) between a group of thousands. But you can use the custom symbol in the number_format function. Example 2 will show you a technique on how to use custom symbols.

The important thing is that changing the number structure using the “number_format” function converts numbers into a String. So, You should manipulate all mathematical calculations before using the number_format function.

Number Format Example: 01

// define number

$amount = 1257459.7398;

// format number with commas and 2 decimal places

// result: "1,257,459.74"

$new_number = number_format($amount, 2);

echo $new_number;

Number Format Example:  02

// define number

$amount = 1957559.7398;

// format number with custom separator

// result: "1'957'559,74"

$new_number = number_format($amount, 2, ',', '\'');

echo $new_number;

Enjoy!

Was this information helpful? What other tips would you like to read about in the future? Share your comments, feedback, and experiences with us by commenting below!

Total 0 Votes
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Comment

Back To Top
Cool Ajax - Web Development Tutorial Blog
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.