Pages: [1]   Go Down
Author Topic: Right justify  (Read 1945 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would like to right justify (with space padding), a long integer in a 5 character fixed length string that can be sent out with the serial print command.

I know I likely need to use sprintf but the syntax escapes me.

What is the elegant way to do this?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 70
Posts: 2171
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You could use if's

Code:
if a >= 100
 pad 1
else if a >= 10
 pad 2
else
 pad 3

itoa after padding

or use itoa and fill a buffer, count its length and shift right by appropriate spaces.

just a few thoughts
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49363
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would like to right justify (with space padding), a long integer in a 5 character fixed length string that can be sent out with the serial print command.
If the value in the long fits in 5 spaces, does it really need to be a long?

%nd should work, where n is the number of characters to use.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would like to right justify (with space padding), a long integer in a 5 character fixed length string that can be sent out with the serial print command.
If the value in the long fits in 5 spaces, does it really need to be a long?

%nd should work, where n is the number of characters to use.

Yes some values are >32768.

%nd was easy to find but I am struggling with defining my variables to do this and then getting result into serial print command.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49363
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but I am struggling with defining my variables to do this and then getting result into serial print command.
Show some code. It should be very simple.
Code:
long val = 99847;

char buff[10];
sprintf(buff, "%5d", val);
Serial.print(buff);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, syntax works now.

I was trying to do this:

buff = sprintf("%5d", val);

BUT, using this code:

void loop()
{
  lcd.setCursor(0,0);
  long val = 99847;
  char buff[10];
  sprintf(buff, "%5d", val);
  lcd.print(buff);
  lcd.print(':');
}

The LCD prints -31225:

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49363
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You'll need to look up the correct type to use - d is for ints. Apparently, you need a different type for longs.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You'll need to look up the correct type to use - d is for ints. Apparently, you need a different type for longs.

From what I can tell there is no 'long' type specifier.  Here is the list:

A type specifier that says what type the argument data should be treated as. Possible types:
% - a literal percent character. No argument is required.
b - the argument is treated as an integer, and presented as a binary number.
c - the argument is treated as an integer, and presented as the character with that ASCII value.
d - the argument is treated as an integer, and presented as a (signed) decimal number.
e - the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of significant digits (one less).
E - like %e but uses uppercase letter (e.g. 1.2E+2).
u - the argument is treated as an integer, and presented as an unsigned decimal number.
f - the argument is treated as a float, and presented as a floating-point number (locale aware).
F - the argument is treated as a float, and presented as a floating-point number (non-locale aware). Available since PHP 4.3.10 and PHP 5.0.3.
g - shorter of %e and %f.
G - shorter of %E and %f.
o - the argument is treated as an integer, and presented as an octal number.
s - the argument is treated as and presented as a string.
x - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters).
X - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).

Am I out of luck?
Logged

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try %5ld
Logged

There's always a better way!

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

YES!

The ld worked!

Thanks!
Logged

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Or, if you don't want the overhead of sprintf(), you can do something like:

Code:
/*============================================================================*/
/* Convert unsigned long value to d-digit decimal string in local buffer      */
/*============================================================================*/
char *u2s(unsigned long x,unsigned d)
{  static char b[16];
   char *p;
   unsigned digits = 0;
   unsigned long t = x;

   do ++digits; while (t /= 10);
   // if (digits > d) d = digits; // uncomment to allow more digits than spec'd
   *(p = b + d) = '\0';
   do *--p = x % 10 + '0'; while (x /= 10);
   while (p != b) *--p = ' ';
   return b;
}
Logged

There's always a better way!

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That is greek to me right now but it looks very nice.

How do I find out how much overhead the sprintf addition to my code costs me?

Thanks again.
Logged

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Write a minimal sketch that compiles cleanly both with and without a call to sprintf() and compare the compiled file sizes. When I did that I found that sprintf() overhead to be about 1582 bytes - but I didn't try to identify how much of that was SRAM and how much was FLASH.
Logged

There's always a better way!

Pages: [1]   Go Up
Jump to: