0

I've been at this all day, and I just can't seem to wrap my mind around how to use sprintf() to set a width for my string so that my characters are right justified.

What I'm trying to do is scooch my output over to the right on my touchscreen when the value is positive.
My output string is always a predictable length, so it seems like it should be easy.
I tried concatenating a string with just a space in it, and every other way I could think of to add a space to the beginning of my string. No luck.

char stringval[8] = 1.08;
char juststringval[8];
            if (hashpos > 0 && hashpos < 10) { 

                sprintf(juststringval,  " %5s" , stringval);
                Serial.println(juststringval);
            }

output:

1.08 (no spaces in front)

In case you're curious why I need to use a string, my touchscreen will print floats, but my library won't rotate them properly.
Only strings can be rotated the way I need them.

Isn't there some simple way of doing this?

EDIT:

It seems I can use the width trick as long as I include some character besides an empty space.
For example:

void setup()
{
char stringval[33] = "1.08";
char juststringval[33];
char space[] = "rr";         ////THIS WORKS: OUTPUT IS  rr      1.08
float hashpos = 1.08;
if (hashpos > 0 && hashpos < 10) {

    sprintf(juststringval, "%s  %7s", space, stringval);
    Serial.println(juststringval);
}
}

void setup()
{
char stringval[33] = "1.08";
char juststringval[33];
char space[] = " ";     ////THIS DOESNT WORK. OUTPUT IS 1.08 LEFT JUSTIFIED
float hashpos = 1.08;
if (hashpos > 0 && hashpos < 10) {

    sprintf(juststringval, "%s  %7s", space, stringval);
    Serial.println(juststringval);
}
}

But I can't live with other characters in my string.
Isn't there something I can do?

11
  • Right justification is the default. Have you tried printing another string above or below it to compare? May 24, 2016 at 0:28
  • Yes, and the default is left justification. When you print something on a new line, it starts on the left, just like words on a page. May 24, 2016 at 0:33
  • Does it work if you supply a left-padded string? May 24, 2016 at 0:41
  • No, it's always as far over to the left as possible. Both in my serial port and on my touchscreen. Even if I artificially insert spaces into "stringval" May 24, 2016 at 0:46
  • Printing in general starts at the left, but within the field described by the format spec - %5s in your case, the string will be right justified within that 5-char field (assuming the string is 5 characters or less). How wide is your screen? If you're trying to right-justify to the width of a 20-char screen, f/ex, you'll need a (20+1) char buffer, and execute:sprintf(buffer, "%20s", stringval); Serial.print(buffer); This will print a line of 20 characters, space padded on the left, ending in your "stringval".
    – JRobert
    May 24, 2016 at 0:48

1 Answer 1

2

The mistake in question and in comments (except of Bigoldsofty's comment), is missing character for justifying. This character should be in format string before the required width. "% 5s" not "%5s"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.