skip to Main Content

I am extremely new to PHP, and I am having some issues with the number_format() function.

I am performing a calculation which is, correctly, returning this result: 6215.
However, I want this value to be displayed/echoed as 62.15. I have played around with the number_format() function to no avail.

Any help would be greatly appreciated!

2

Answers


  1. This was directly pulled from the php.net documentation, which I think is really great.
    https://www.php.net/manual/en/function.number-format.php

    
    <?php
    
    $number = 1234.56;
    
    // english notation (default)
    $english_format_number = number_format($number);
    // 1,235
    
    // French notation
    $nombre_format_francais = number_format($number, 2, ',', ' ');
    // 1 234,56
    
    $number = 1234.5678;
    
    // english notation without thousands separator
    $english_format_number = number_format($number, 2, '.', '');
    // 1234.57
    
    ?>
    
    
    Login or Signup to reply.
  2. You have 2 potential tasks here.

    1.) You want to change your number (divide by 100)

    <?php
      $number = 6215;
      $number = $number / 100;
      echo $number;
    ?>
    Renders as 62.15
    

    2.) You may want additional formatting to make a "pretty" number

    From the docs: https://www.php.net/manual/en/function.number-format.php

    // Function signature syntax
    number_format(
        float $num,
        int $decimals = 0,
        ?string $decimal_separator = ".",
        ?string $thousands_separator = ","
    )
    
    <?php
      $number = 1006215.56;
      $pretty_number = number_format($number, 2, '.', '');
      echo $pretty_number;
    ?>
    Renders as 1,006,215.56
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search