Let's say I have a variable defined as float
x = 3.52;
I want to display this number on the OLED display with 1 leading zero, how can I do that with u8glib?
I mean the display number should look like this: "03.52"
Yep, it has nothing to do with the lib... You can write a function for it
Just made a quick one. Keep forgetting to make a nice library for it. And I never made a float version. Float is evel misused most of the time. Bigger fan of fixed point notation.
void printFixedWidth(Print &out,float in, int width, byte decimals){
float temp = in;
//do we need room for a minus?
if(in < 0){
width--;
}
//compensate for after the decimal and the decimal
width -= decimals + 1;
//no room left?
if(width < 0){
width = 0;
}
//let's check width of variable efore decimal
while(temp > 10 && width){
temp /= 10;
width--;
}
//now let's print it
//is it negative?
if(in < 0){
out.print('-');
}
//fill it up
while(width){
out.print('0');
width--;
}
//now print the number
out.print(abs(in), decimals);
}
I would certainly agree that the Arduino Print formatting is a pain.
But the C++ stream approach is pretty horrible too.
Regular C printf() is considerably easier to use but is not set up for floats in the Arduino Build.
Yes, your helper function is useful.
My "simple" one-liner is probably sufficient in 99.5% of Arduino sketches. e,g. displaying fixed width value on a 16x2 LCD.