Created
August 2, 2014 08:46
-
-
Save paddyzab/b8e06bff13188dadb3a2 to your computer and use it in GitHub Desktop.
lcdPrintFloat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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