Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. void printDouble( double val, byte precision){
  2. // prints val with number of decimal places determine by precision
  3. // precision is a number from 0 to 6 indicating the desired decimial places
  4. // example: lcdPrintDouble( 3.1415, 2); // prints 3.14 (two decimal places)
  5.  
  6. if(val < 0.0){
  7. Serial.print('-');
  8. val = -val;
  9. }
  10.  
  11. Serial.print (int(val)); //prints the int part
  12. if( precision > 0) {
  13. Serial.print("."); // print the decimal point
  14. unsigned long frac;
  15. unsigned long mult = 1;
  16. byte padding = precision -1;
  17. while(precision--)
  18. mult *=10;
  19.  
  20. if(val >= 0)
  21. frac = (val - int(val)) * mult;
  22. else
  23. frac = (int(val)- val ) * mult;
  24. unsigned long frac1 = frac;
  25. while( frac1 /= 10 )
  26. padding--;
  27. while( padding--)
  28. Serial.print("0");
  29. Serial.println(frac,DEC) ;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement