Using number_format without changing decimal

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tencip

    Using number_format without changing decimal

    Hi everyone,

    So, I'm running data through an array, and having a little difficulty
    in using the number_format function. You see, I'm trying to get a
    comma to appear for the thousandths place, however, whenever i use the
    number_format function, it also forces rounding of the decimal.

    Does anyone know how I can place commas in the thousandths place
    WITHOUT doing any decimal rounding? I have some datapoints with two
    decimal places, some with four, and others with none. I don't want to
    have the function affect any of these decimal places.

    Any suggestions?

    -Tencip

  • Alexey Kulentsov

    #2
    Re: Using number_format without changing decimal

    tencip wrote:[color=blue]
    > So, I'm running data through an array, and having a little difficulty
    > in using the number_format function. You see, I'm trying to get a
    > comma to appear for the thousandths place, however, whenever i use the
    > number_format function, it also forces rounding of the decimal.
    >
    > Does anyone know how I can place commas in the thousandths place
    > WITHOUT doing any decimal rounding? I have some datapoints with two
    > decimal places, some with four, and others with none. I don't want to
    > have the function affect any of these decimal places.
    >
    > Any suggestions?[/color]

    preg_replace('/0+$/','',number_for mat($number,4,' .',','))

    Comment

    • Andy Hassall

      #3
      Re: Using number_format without changing decimal

      On 11 Sep 2005 18:36:09 -0700, "tencip" <tencip@yahoo.c om> wrote:
      [color=blue]
      >So, I'm running data through an array, and having a little difficulty
      >in using the number_format function. You see, I'm trying to get a
      >comma to appear for the thousandths place, however, whenever i use the
      >number_forma t function, it also forces rounding of the decimal.
      >
      >Does anyone know how I can place commas in the thousandths place
      >WITHOUT doing any decimal rounding? I have some datapoints with two
      >decimal places, some with four, and others with none. I don't want to
      >have the function affect any of these decimal places.[/color]

      This is a bit gruesome but it's the first thing that came to mind:

      <?php
      function number_format_k eep_decimals($n um)
      {
      if ($point = strrpos($num, '.'))
      {
      return number_format($ num, strlen($num) - $point - 1);
      }
      else
      {
      return number_format($ num);
      }
      }

      $nums = array(1000.2, 2000.34, 5000.678, 9000);

      foreach ($nums as $num)
      {
      print $num . ' -> ' .number_format_ keep_decimals($ num);
      print "<br>";
      }
      ?>

      --
      Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
      http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

      Comment

      Working...