How to output numbers 'right justified' w/o leading zeros on u8x8 library

Hi!
I am using the ollikraus u8g2 library in the u8x8 mode on an OLED Display. The system uses a Arduino nano.
I would like to output a number that can hold values from 0 to 100 (int).
If I do so, the library will left justify. It leaves me with the problem that when going from '10' to '9' the display will display a 90.
The library is total flickerfree when displaying variables- just that it cant take into account any formatting. At least I hevent´t found a way to do it.
So here is some code:

[color=#24292e]U8X8_SH1106_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);[/color]
[color=#24292e]u8x8.begin();
u8x8.setPowerSave(0);
display();[/color]

And in my function:

u8x8.setFont(u8x8_font_amstrad_cpc_extended_r);
u8x8.setCursor(0, 0);
u8x8.drawString(0,20, String(pwmd).c_str());

Alternative I can use (the draw string does not output variables)

u8x8.print(pwmd);

BUT: When I go from 10 to 9 it will display a 90
I dont want to have 09, i would like to display 9 only.
How can I achieve this?

In a former discussion someone said use:

u8x8.drawString(0,20, "   "); // 3 spacers to cover 100

but that would get me a lot of flickering when changing the value,
same with the clear() command.
I would like to format my string, but I cant get the library to accept any printf or sprintf commands. (u8x8.printf...does not work. So I tried:

char buffer [50];
  int n, a=5, b=3;
  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);

  //u8x8.drawString(0,20, pwmd);
  // printf ("Width trick: %*d \n", 5, 10);

  u8x8.drawString(0,20, String(n).c_str())

But that does only output the pointer the string (13)- not the string itself.
I feel a bit givven up on this - I think somehow it is something that we all solved 100 times when working with LCD displays, but somehow I can´t get working on this OLED with the prominent u8g2 lib.
So maybe someone could guide me a bit?

The u8x8 fonts are not pretty but they are monospaced. So a space character takes the same real estate as a digit.

void loop(void)
{
    u8x8.setFont(u8x8_font_amstrad_cpc_extended_r);
    char buf[10];
    sprintf(buf, "%3d", random(0, 110));
    u8x8.drawString(0, 0, buf);
    u8x8.refreshDisplay();		// only required for SSD1606/7
    delay(2000);
}

You can use sprintf() or dtostrf() to format numbers. Much easier than print() or any String manipulation.

David.

Oh, that works!
But..why? :slight_smile: Why can u8x8.drawString suddenly decode a variable? It could not when you do something like
int buf=8;
u8x8.drawString(0, 0, buf);

Is it because it is a char array?

U8g2 is a C++ wrapper on a C library.

C only has one function per set of arguments. e.g. drawString() expects ints for x, y and char* for the "string"

C++ can overload functions e.g. xxx.print(int) and xxx.print(char*)
You need to study a C++ textbook to understand.

Both sprintf() and dtostrf() are also C functions. They can both pad with space or 0. And give you a nicely formatted display.

And no, I don't think you would see a noticeable flicker with print(). It is just a bit messy when you have to format by hand.

David.

Ahh, I see..thanks!