C18 sprintf

Status
Not open for further replies.

AtomSoft

Well-Known Member
Hey guys just want to ask what could be a simple question. Im using sprintf to output numbers into string format which is working well.

But i would like 2 characters. Like when i have a char and the number is anywhere from 0 to 9 i get 0 to 9 but i would like to get 00-09.. get it?

I usually get:
0
1
2
3
4
5
6
7
8
9
10 - FIRST 2 CHARACTERS

I would like:

00
01
02
03
04
05
06
07
08
09
10

Is it possible to some how force this in sprintf? or should i simply:

Code:
			if(Data< 10){
				sprintf(Data2,"0%d\0",Data);
			} else {
				sprintf(Data2,"%d\0",Data);
			}
 
printf("%03d\n",b);

In this example the 0 following the % indicates to show leading zeros.
The 3 is the field width
The d is decimal.

So if b was 8
it would print
008
 
So :
b = 4
printf("%02d\n",b);

Would output : 04

b = 12
printf("%02d\n",b);

Would output : 12

Correct? or would b=12 output 012?
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…