473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Jul 17 '05 #1
6 38183
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
"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
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bob Bryan | last post by:
I am using a select statement to obtain a result set back with aggregated data. The problem is that I am seeing column data with 11 to 13 digits after the decimal point. I tried using the STR...
12
by: Don | last post by:
With the following, I'm trying to display numberOne and numberTwo, each as a two digit character group, such as "nn". If numberOne or numberTwo is less than 10, "bn" will be displayed (where "b"...
2
by: MLH | last post by:
Using A97, I have tables with 10-char text phone number fields to allow entry of xxxyyyyyyy phone numbers - where xxx is the 3-digit area code and yyyyyyy is the 7-digit phone number. I wish to...
8
by: Nathan Waddington | last post by:
Hello Everyone, My program receives a string representation of a 64bit hex number that needs to be displayed as the decimal number it represents. The compiler that is being used is Dynamic C...
1
by: Michael | last post by:
Hi there I am still working with Access97/VBA (will change to 2003 soon) I use a lot of listboxes displaying numbers and they are always left justified. I'd like them to be right justified.. But...
2
by: Michael C via AccessMonster.com | last post by:
I have several tables that have numbers displaying 2 digits to the right of the decimal. However, when I append these into a common table, all numbers are suddenly rounded up to the nearest whole...
10
by: LuTHieR | last post by:
Hi, I'm reading a string of numbers from a file (using Borland C++ Builder 6), and I'm doing it like this: first I use FileRead to store all the data in the file to a char* variable (appropriately...
3
blyxx86
by: blyxx86 | last post by:
Interesting question/thought/problem ... I want to use some type of input mask or format to display only a certain set of numbers within a string of numbers. For example.. I want to be able to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.