olikraus / u8glib

Arduino Monochrom Graphics Library for LCDs and OLEDs
https://github.com/olikraus/u8glib/wiki
Other
1.24k stars 313 forks source link

get width of 'int' #477

Open snapedumbledore opened 6 years ago

snapedumbledore commented 6 years ago

I would like to know how can i get the width of 'int' type, not a string. Thanks.

olikraus commented 6 years ago

This is more a C/C++ question, not a u8g2 related question. Nevertheless, the bytewidth can be calculated with "sizeof(int)"

t3chguy commented 6 years ago

Unless of course he means get the width of a numeric character in a font given the number as an int

snapedumbledore commented 6 years ago

Emmmm.Thanks. I want to calculate the 'x' width which is int。(int x = 0,and x will be increased or decreased) I know i can get a string width through u8g.getStrWidth @olikraus

olikraus commented 6 years ago

There is no other option: You have to convert the number (int) to a string. During this conversion you have to decide whether to include a minus sign, leading zeros, an additional % sign and which base you want to use (10/decimal, 16 hexadecimal, etc). All these decision during the string conversions will change the size of the number. So, i guess it will be best to use "sprintf" with proper conversion options and then apply the resulting string to getStrWidth().