By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

Displaying numbers with 2 digits

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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;
Jul 17 '05 #2

P: n/a
"Michael" <mi********@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.

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

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 17 '05 #3

P: n/a
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
http://www.php.net/manual/en/languag...arithmetic.php

I think you want the

.. string operator
http://www.php.net/manual/en/languag...ors.string.php
<?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
http://www.php.net/number_format
http://www.php.net/printf
HTH

--
..sig
Jul 17 '05 #4

P: n/a
Kevin Thorpe <ke***@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
http://www.fuhr.org/~mfuhr/
Jul 17 '05 #5

P: n/a
> 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 !
Jul 17 '05 #6

P: n/a
On Mon, 24 Nov 2003 18:21:50 +0100, Michael wrote:
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 !

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

Regards,

Ian

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

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.