Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void printDouble( double val, byte precision){
- // prints val with number of decimal places determine by precision
- // precision is a number from 0 to 6 indicating the desired decimial places
- // example: lcdPrintDouble( 3.1415, 2); // prints 3.14 (two decimal places)
- if(val < 0.0){
- Serial.print('-');
- val = -val;
- }
- Serial.print (int(val)); //prints the int part
- if( precision > 0) {
- Serial.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--)
- Serial.print("0");
- Serial.println(frac,DEC) ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement