Pages: [1]   Go Down
Author Topic: Fomratting numbers  (Read 321 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 81
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there an easy way to format numbers?
I map the value of a potentiometer to 0 - 24 and if the value is < 10 it should do a leading 0.
So that it would print
00
01
02
03
..
09
10
11
and so on.

Of course I could do a simple if the value is < 10 but I thought maybe there is a built in function that does this.

Thanks
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Make a temporary buffer (EDIT: and use sprintf) or use this: http://arduino.cc/forum/index.php/topic,120440.0.html
« Last Edit: January 06, 2013, 06:34:57 pm by WizenedEE » Logged

Nova Scotia
Offline Offline
Full Member
***
Karma: 4
Posts: 204
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I did something like this for my real time clock to display a 01,02,02...
It has a lot of other junk in there but all it really does is push the number to the right and jam a zero in front if less than 0.

  char second[2];
  int tmpsecond = bcdToDec(Wire.receive());

  if(tmpsecond < 10)
  {
   second[1] = second[0];
   second[0] = '0';
  }
  uoled.TextGraphic(112,10,SMALL_FONT,0xFFFF,1,1,second,1);
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13742
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


a simple  if the value is < 10  construct  is far faster than sprintf which can handle many formatting commands
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: