Skip to content

Instantly share code, notes, and snippets.

@wware
Created July 25, 2015 23:59
Show Gist options
  • Save wware/aa3fb30b34b06a2314b6 to your computer and use it in GitHub Desktop.
Save wware/aa3fb30b34b06a2314b6 to your computer and use it in GitHub Desktop.
An Arduino function to print a double-precision floating point number via Serial. Not exhaustively tested, use at your own risk.
void print_double(double val)
{
char buf[10];
int neg = (val < 0);
int exponent = 0;
if (val == 0.0) {
Serial.print("0.0");
return;
}
if (neg)
val = -val;
while (val >= 10.0) {
val *= 0.1;
exponent++;
}
while (val < 1.0) {
val *= 10.0;
exponent--;
}
if (neg)
Serial.print("-");
Serial.print(int(val));
Serial.print(".");
sprintf(buf, "%06u",
(unsigned int) ((val - int(val)) * 1000000));
Serial.print(buf);
if (exponent != 0) {
Serial.print('e');
Serial.print(exponent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment