mirror of
https://github.com/arduino/Arduino.git
synced 2024-11-30 11:24:12 +01:00
Adding support for printing floats and doubles (defaulting to 2 decimal places)
This commit is contained in:
parent
950789ab74
commit
2661608ba4
@ -22,6 +22,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <math.h>
|
||||||
#include "wiring.h"
|
#include "wiring.h"
|
||||||
|
|
||||||
#include "Print.h"
|
#include "Print.h"
|
||||||
@ -78,6 +79,11 @@ void Print::print(long n, int base)
|
|||||||
printNumber(n, base);
|
printNumber(n, base);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Print::print(double n)
|
||||||
|
{
|
||||||
|
printFloat(n*100, 2);
|
||||||
|
}
|
||||||
|
|
||||||
void Print::println(void)
|
void Print::println(void)
|
||||||
{
|
{
|
||||||
print('\r');
|
print('\r');
|
||||||
@ -132,6 +138,12 @@ void Print::println(long n, int base)
|
|||||||
println();
|
println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Print::println(double n)
|
||||||
|
{
|
||||||
|
print(n);
|
||||||
|
println();
|
||||||
|
}
|
||||||
|
|
||||||
// Private Methods /////////////////////////////////////////////////////////////
|
// Private Methods /////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void Print::printNumber(unsigned long n, uint8_t base)
|
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] :
|
'0' + buf[i - 1] :
|
||||||
'A' + buf[i - 1] - 10));
|
'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;
|
||||||
|
}
|
||||||
|
}
|
@ -32,6 +32,7 @@ class Print
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
void printNumber(unsigned long, uint8_t);
|
void printNumber(unsigned long, uint8_t);
|
||||||
|
void printFloat(double, uint8_t);
|
||||||
public:
|
public:
|
||||||
virtual void write(uint8_t);
|
virtual void write(uint8_t);
|
||||||
void print(char);
|
void print(char);
|
||||||
@ -42,6 +43,7 @@ class Print
|
|||||||
void print(long);
|
void print(long);
|
||||||
void print(unsigned long);
|
void print(unsigned long);
|
||||||
void print(long, int);
|
void print(long, int);
|
||||||
|
void print(double);
|
||||||
void println(void);
|
void println(void);
|
||||||
void println(char);
|
void println(char);
|
||||||
void println(const char[]);
|
void println(const char[]);
|
||||||
@ -51,6 +53,7 @@ class Print
|
|||||||
void println(long);
|
void println(long);
|
||||||
void println(unsigned long);
|
void println(unsigned long);
|
||||||
void println(long, int);
|
void println(long, int);
|
||||||
|
void println(double);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user