Skip to content

Instantly share code, notes, and snippets.

@paddyzab
Created August 2, 2014 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paddyzab/b8e06bff13188dadb3a2 to your computer and use it in GitHub Desktop.
Save paddyzab/b8e06bff13188dadb3a2 to your computer and use it in GitHub Desktop.
lcdPrintFloat
void lcdPrintFloat( float val, byte precision) {
// prints val on a ver 0012 text lcd with number of decimal places determine by precision
// precision is a number from 0 to 6 indicating the desired decimial places
// example: lcdPrintFloat( 3.1415, 2); // prints 3.14 (two decimal places)
if(val < 0.0){
lcd.print('-');
val = -val;
}
lcd.print ((long)val); //prints the integral part
if( precision > 0) {
lcd.print("."); // print the decimal point
unsigned long frac;
unsigned long mult = 1;
byte padding = precision -1;
while(precision--)
mult *=10;
if(val >= 0)
frac = (val - int(val)) * mult;
else
frac = (int(val)- val ) * mult;
unsigned long frac1 = frac;
while( frac1 /= 10 )
padding--;
while( padding--)
lcd.print("0");
lcd.print(frac,DEC) ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment