Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use number_format() function in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 417
    Comment on it

    Hi Readers,
    Welcome to FindNerd, today we are going to discuss how to use a number_format() function in PHP ?

    Basically, a number_format() function is used to format numbers with grouped thousands. This function is very easy to implement.

    Number_format() function only supports one, two, or four parameters (does not accept three arguments).


    Syntax of number_format() function

    number_format(number, decimal_values, decimal_point, thousand_separator)

     

    let explain one by one

    number : This is a required parameter and this is the number which has to be formatted.This is formatted with comma (,).

    decimal_values :This is an optional parameter and specifies the number of decimal values after decimal_point.This is formatted with comma (.).

    decimal_point :This is a optional parameter and specifies the string used as decimal point separator.

    thousand_separator : This is a optional parameter and specifies the string used as thousand separator.

    You can see below examples:

    1.

    <?php
       //define a variable of number
       $number = 256321.25;
       echo number_format($number);
    ?>
    

    Output : 256,321

    Because, here print only number value, So, number_format() remove all numbers which will come after decimal and just print value before decimal.

     

    2.

    <?php
       //define a variable of number
       $number = 256321.2525;
       echo number_format( $number, 2);
    ?>
    

    Output :256,321.25
    Because, here we are printing number value and decimal value which length is 2,
    So,number_format() remove the all number which will come after 2 number decimal point.

     

    3.

    <?php
        //define a variable of number
        $number = 256321.2525;
        echo number_format( $number, 2, '/', '-');
      ?>

    Output : 256-321/25

    Because, here we are printing number value with arguments.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: