Problem with numbers less than 1 when using printf to format floating-pointnumbers

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

    Problem with numbers less than 1 when using printf to format floating-pointnumbers

    The following code:

    <?php
    header("Content-Type: text/plain");
    $numbers = array(3.714, 0.7142857142857 143, 9.667, 6.333, 7, 6, 8, 2, 3,
    -2, 0, -6.6, 2.25, 4.333, -8, 3, 3.141592654, 5.5, 6.5, 6.857);
    foreach ($numbers as $value) printf("% 20.2f\n", $value);
    ?>

    produces the following output:

    3.71
    0.71
    9.67
    6.33
    7.00
    6.00
    8.00
    2.00
    3.00
    -2.00
    0.00
    -6.60
    2.25
    4.33
    -8.00
    3.00
    3.14
    5.50
    6.50
    6.86

    It seems that the width specifier of 20 refers to the total width,
    except for numbers less than 1, where it refers to the number of
    characters before the decimal point. It doesn't matter whether space
    padding is used or not, or whether I output to an html file using the
    <pre> element. Is there a quick way of aligning all the numbers? I know
    I could use "if" to test whether a number is less than 1 and then
    specifying a width of 17 character.

    Thanks for any help.

    Paul
  • Steve

    #2
    Re: Problem with numbers less than 1 when using printf to format floating-point numbers


    Paul wrote:[color=blue]
    > It seems that the width specifier of 20 refers to the total
    > width, except for numbers less than 1, where it refers to the number[/color]
    of[color=blue]
    > characters before the decimal point.[/color]

    What version of PHP are you using? Tried v4.3.4 and v4.3.9, printf()
    works as documented.

    ---
    Steve

    Comment

    • Pedro Graca

      #3
      Re: Problem with numbers less than 1 when using printf to format floating-point numbers

      Paul wrote:[color=blue]
      > The following code:
      >
      > <?php
      > header("Content-Type: text/plain");
      > $numbers = array(3.714, 0.7142857142857 143, 9.667, 6.333, 7, 6, 8, 2, 3,
      > -2, 0, -6.6, 2.25, 4.333, -8, 3, 3.141592654, 5.5, 6.5, 6.857);
      > foreach ($numbers as $value) printf("% 20.2f\n", $value);
      > ?>
      >
      > produces the following output:
      >
      > 3.71
      > 0.71[/color]
      <snip>

      For me (Linux, PHP 4.3.9) it produces the expected result.


      Hmmm ... seems you're running PHP 4.3.7 :-)


      --
      Mail to my "From:" address is readable by all at http://www.dodgeit.com/
      == ** ## !! ------------------------------------------------ !! ## ** ==
      TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
      may bypass my spam filter. If it does, I may reply from another address!

      Comment

      • Paul

        #4
        Re: Problem with numbers less than 1 when using printf to formatfloating-point numbers

        Steve wrote:
        [color=blue]
        > Paul wrote:
        >[color=green]
        >>It seems that the width specifier of 20 refers to the total
        >>width, except for numbers less than 1, where it refers to the number[/color]
        >
        > of
        >[color=green]
        >>characters before the decimal point.[/color]
        >
        >
        > What version of PHP are you using? Tried v4.3.4 and v4.3.9, printf()
        > works as documented.
        >
        > ---
        > Steve
        >[/color]

        4.3.8

        I presume that "as documented" means that the width specifier refers to
        the (minimum) number of characters in total and not those before the
        decimal point. I'll now try installing another version of php on my
        machine and see what happens.

        Paul

        Comment

        Working...