1

I have a similar problem than the discussed here : How to display variables on 0.96" OLED with u8glib library?

I'd like to know how can I show float variables in place of int variables. What should i modify in this part of my code ?

char buf[9];
sprintf (buf, "%d", a);
u8g.drawStr(18, 3, buf);

} while( u8g.nextPage() );

delay(200);

}

Thanks in advance =D

3
  • Is this code working for you? If not, what happens wrong?
    – chrisl
    Sep 3, 2019 at 18:15
  • yes, it's working, but just with int variables =/. when i try float ones it do not work. Sep 3, 2019 at 18:18
  • What means "does not work"? Please describe what actually happens
    – chrisl
    Sep 3, 2019 at 18:19

2 Answers 2

1

The printf group of functions in the Arduino library don't implement float and double conversions. For those, you need the function

char *dtostrf(double dval, int8_t width, uint8_t prec, char *buf);
  Convert the double value passed in dval into an ASCII string stored in
  buf. Conversion is done as "[-]d.ddd". The minimum field width of the output
  string (including the '.' and the possible sign for negative values) is given
  in width, and prec determines the number of digits after the decimal. width
  is signed, negative for left adjustment.
  Returns buf.

, which you'll need to call for each float you want to convert.

Note that you could convert several floats into strings, upfront, each into its own character buffer, then use sprintf to format the final buffer - remembering that in sprintf's format string, those buffers contain character strings, which you'll need to specify with '%s' format-specs.

0

The u8glib library provides the Arduino's standard print() method. You can the write:

u8g.setPrintPos(18, 3);
u8g.print(a);

and it should work for mostly any type of a.

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.