Displaying numbers with 2 digits

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default 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


    Michael Guest

  2. Similar Questions and Discussions

    1. Page Numbers not all Displaying
      In one particular document I am working on, I can't seem to get some of my page numbers to display. I have applied the master to those pages. Also,...
    2. stripping non digits
      Can anyone point me to a routine to strip anything but digits and a decimal point out of a string? Thx, Ike
    3. Displaying numbers in forms
      Is there any way to display a number in one field as simply a number and to have that same number spelled out in another linked (or something) field?...
    4. displaying page numbers when scrolling
      When I first installed Acrobat and scrolled some document in it, moving along the scroll bar using the mouse cursor (dragging the scroll bar down, so...
    5. Recognizing digits
      Hi, I have a field on the stage named, "typeBox", and a button named "unlock". If a user types into the field, a number which is larger than 9,...
  3. #2

    Default Re: Displaying numbers with 2 digits

    Michael wrote:
    > 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 ?
    $c = $a+$b;
    printf('%2u',$c);

    ...or..

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


    Kevin Thorpe Guest

  4. #3

    Default Re: Displaying numbers with 2 digits

    "Michael" <michaperso@easynet.fr> writes:
    > 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).
    See the documentation for printf() and sprintf() in the PHP manual.

    [url]http://www.php.net/manual/en/ref.strings.php[/url]
    [url]http://www.php.net/manual/fr/ref.strings.php[/url]

    --
    Michael Fuhr
    [url]http://www.fuhr.org/~mfuhr/[/url]
    Michael Fuhr Guest

  5. #4

    Default Re: Displaying numbers with 2 digits

    Michael wrote:
    > 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 ?
    + is an arithmetic operator
    [url]http://www.php.net/manual/en/language.operators.arithmetic.php[/url]

    I think you want the

    .. string operator
    [url]http://www.php.net/manual/en/language.operators.string.php[/url]


    <?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
    [url]http://www.php.net/number_format[/url]
    [url]http://www.php.net/printf[/url]


    HTH

    --
    ..sig
    Pedro Graca Guest

  6. #5

    Default Re: Displaying numbers with 2 digits

    Kevin Thorpe <kevin@pricetrak.com> writes:
    > Michael wrote:
    > >
    > > 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 ?
    >
    > $c = $a+$b;
    > printf('%2u',$c);
    >
    > ..or..
    >
    > $c = sprintf('%2u',$a+$b);
    > echo $c;
    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
    [url]http://www.fuhr.org/~mfuhr/[/url]
    Michael Fuhr Guest

  7. #6

    Default Re: Displaying numbers with 2 digits

    > 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.
    It's "%02d"

    Thanx folks !


    Michael Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139