How to use number format in PHP

In this article I will explain how the number_format() function can be used in PHP.
  • 2330

number_format() function in PHP

  • The number_format() function is used to format a number with grouped thousands.
  • The number_format() function supports one, two, or four parameters (not three).

Syntax

number_format(number,decimals,decimalpoint,separator)

Parameter

  • number numberis required parameter. The number to be formatted. the number will be formatted without decimals and with comma (,),  If no other parameters are set.
  • decimals decimal is optional parameter. count the decimals. the number will be formatted with a dot (.), If this parameter is set.
  • decimalpoint decimalpoint is optional parameter. determine for what string to use for decimal point.
  • separator separator is optional parameter. Only the first character of separator is used.

Example

The following example show to how the number_format() function can be used in PHP.

<html>

<body>

<h3 style="color: green;">number_format() function example in PHP</h3>

    <?php

    $num=500000;

    echo number_format($num).'<br>';

    echo number_format($num, 2).'<br>';

    echo number_format($num, 3).'<br>';

    echo number_format($num, 2, ',', '.');

    ?>

</body>

</html>

 

Output

number-format-php.gif
You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.