aboutsummaryrefslogtreecommitdiff
path: root/cores/arduino/Print.cpp
diff options
context:
space:
mode:
authorDavid A. Mellis <d.mellis@arduino.cc>2008-11-26 14:15:24 +0000
committerDavid A. Mellis <d.mellis@arduino.cc>2008-11-26 14:15:24 +0000
commit1d861783877eda29996a08184e62a181f96d6f75 (patch)
tree0c6940b19a7066e722132346d47120aa01bd9b7f /cores/arduino/Print.cpp
parent051eb371a6c277a9df95fd88f8a5abdd7fa60440 (diff)
Adding support for printing floats and doubles (defaulting to 2 decimal places)
Diffstat (limited to 'cores/arduino/Print.cpp')
-rwxr-xr-xcores/arduino/Print.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp
index feff940..143f15d 100755
--- a/cores/arduino/Print.cpp
+++ b/cores/arduino/Print.cpp
@@ -22,6 +22,7 @@
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
+#include <math.h>
#include "wiring.h"
#include "Print.h"
@@ -78,6 +79,11 @@ void Print::print(long n, int base)
printNumber(n, base);
}
+void Print::print(double n)
+{
+ printFloat(n*100, 2);
+}
+
void Print::println(void)
{
print('\r');
@@ -132,6 +138,12 @@ void Print::println(long n, int base)
println();
}
+void Print::println(double n)
+{
+ print(n);
+ println();
+}
+
// Private Methods /////////////////////////////////////////////////////////////
void Print::printNumber(unsigned long n, uint8_t base)
@@ -154,3 +166,21 @@ void Print::printNumber(unsigned long n, uint8_t base)
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
}
+
+void Print::printFloat(double number, uint8_t scale)
+{
+ double mult = pow(10,scale);
+ double rounded = floor(number /mult);
+ double biground = rounded * mult;
+ double remainder = (number - biground);
+ remainder = remainder / mult;
+ print(long(rounded));
+ print(".");
+
+ while (scale--) {
+ double toPrint = floor(remainder * 10);
+ print(int(toPrint));
+ remainder -= (toPrint/10);
+ remainder *= 10;
+ }
+} \ No newline at end of file