Displaying numbers with 2 digits

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

    Displaying numbers with 2 digits

    Hi,

    I have something like :
    $c=$a+$b;
    echo $c,

    The thing is, i want the result to be a 2 digit number (ie 05 and not 5).
    How 2 ?

    Thanks in advance.

    Michael


  • Kevin Thorpe

    #2
    Re: Displaying numbers with 2 digits

    Michael wrote:[color=blue]
    > Hi,
    >
    > I have something like :
    > $c=$a+$b;
    > echo $c,
    >
    > The thing is, i want the result to be a 2 digit number (ie 05 and not 5).
    > How 2 ?[/color]

    $c = $a+$b;
    printf('%2u',$c );

    ...or..

    $c = sprintf('%2u',$ a+$b);
    echo $c;


    Comment

    • Michael Fuhr

      #3
      Re: Displaying numbers with 2 digits

      "Michael" <michaperso@eas ynet.fr> writes:
      [color=blue]
      > I have something like :
      > $c=$a+$b;
      > echo $c,
      >
      > The thing is, i want the result to be a 2 digit number (ie 05 and not 5).[/color]

      See the documentation for printf() and sprintf() in the PHP manual.




      --
      Michael Fuhr

      Comment

      • Pedro Graca

        #4
        Re: Displaying numbers with 2 digits

        Michael wrote:[color=blue]
        > Hi,
        >
        > I have something like :
        > $c=$a+$b;
        > echo $c,
        >
        > The thing is, i want the result to be a 2 digit number (ie 05 and not 5).
        > How 2 ?[/color]

        + is an arithmetic operator


        I think you want the

        .. string operator



        <?php
        $as = "0";
        $bs = "5";
        $cs = "0";

        $an = 0;
        $bn = 5;
        $cn = 0;

        echo $as + $bs; ### 5
        echo $as . $bs; ### 05
        echo $bs + $cs; ### 5
        echo $bs . $cs; ### 50

        echo $an + $bn; ### 5
        echo $an . $bn; ### 05
        echo $bn + $cn; ### 5
        echo $bn . $cn; ### 50
        ?>


        Or, if I misunderstood you, use the number_format() or printf()
        functions




        HTH

        --
        ..sig

        Comment

        • Michael Fuhr

          #5
          Re: Displaying numbers with 2 digits

          Kevin Thorpe <kevin@pricetra k.com> writes:
          [color=blue]
          > Michael wrote:[color=green]
          > >
          > > I have something like :
          > > $c=$a+$b;
          > > echo $c,
          > >
          > > The thing is, i want the result to be a 2 digit number (ie 05 and not 5).
          > > How 2 ?[/color]
          >
          > $c = $a+$b;
          > printf('%2u',$c );
          >
          > ..or..
          >
          > $c = sprintf('%2u',$ a+$b);
          > echo $c;[/color]

          Using %2u results in a leading space, not a leading zero as stated
          in the requirement. See the documentation for sprintf() to find
          out how to get leading zeros.

          --
          Michael Fuhr

          Comment

          • Michael

            #6
            Re: Displaying numbers with 2 digits

            > Using %2u results in a leading space, not a leading zero as stated in
            [color=blue]
            > the requirement. See the documentation for sprintf() to find out how[/color]
            [color=blue]
            > to get leading zeros.[/color]

            It's "%02d"

            Thanx folks !


            Comment

            • Ian.H

              #7
              Re: Displaying numbers with 2 digits

              On Mon, 24 Nov 2003 18:21:50 +0100, Michael wrote:
              [color=blue][color=green]
              >> Using %2u results in a leading space, not a leading zero as stated in[/color]
              >[color=green]
              >> the requirement. See the documentation for sprintf() to find out how[/color]
              >[color=green]
              >> to get leading zeros.[/color]
              >
              > It's "%02d"
              >
              > Thanx folks ![/color]


              The 'u' IIRC, is a symbol for 'UNSIGNED' INTs.



              Regards,

              Ian

              --
              Ian.H [Design & Development]
              digiServ Network - Web solutions
              www.digiserv.net | irc.digiserv.ne t | forum.digiserv. net
              Programming, Web design, development & hosting.

              Comment

              Working...