Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user

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
My output string is always a predictable length, so it seems like it should be easy. I've
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
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
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
Isn't there something I can do?

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've 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?

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?

added 778 characters in body
Source Link

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've 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?

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've 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?

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've 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?

Source Link

Using Sprintf() to left pad a string?

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've 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?