diff options
author | Tevin Zhang <mail2tevin@gmail.com> | 2013-05-29 10:42:07 +0800 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2013-06-06 16:19:34 +0200 |
commit | c8a79d0d0c999997d2ee9cd5b773b5ff6561e130 (patch) | |
tree | 44bc39518d36d085d8827a4fba4eb1f7908ba316 /cores/arduino/WString.cpp | |
parent | 6bef2ada0627be776e463661ccbcdeba5a373f3a (diff) |
add String.toFloat
Diffstat (limited to 'cores/arduino/WString.cpp')
-rw-r--r-- | cores/arduino/WString.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index aab2a54..e19f543 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -100,6 +100,19 @@ String::String(unsigned long value, unsigned char base) *this = buf; } +String::String(float value, int decimalPlaces) +{ + init(); + char buf[33]; + *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); +} + +String::String(double value, int decimalPlaces) +{ + init(); + char buf[33]; + *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); +} String::~String() { free(buffer); @@ -283,6 +296,20 @@ unsigned char String::concat(unsigned long num) return concat(buf, strlen(buf)); } +unsigned char String::concat(float num) +{ + char buf[20]; + char* string = dtostrf(num, 8, 6, buf); + return concat(string, strlen(string)); +} + +unsigned char String::concat(double num) +{ + char buf[20]; + char* string = dtostrf(num, 8, 6, buf); + return concat(string, strlen(string)); +} + /*********************************************/ /* Concatenate */ /*********************************************/ @@ -343,6 +370,19 @@ StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num) return a; } +StringSumHelper & operator + (const StringSumHelper &lhs, float num) +{ + StringSumHelper &a = const_cast<StringSumHelper&>(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, double num) +{ + StringSumHelper &a = const_cast<StringSumHelper&>(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} /*********************************************/ /* Comparison */ /*********************************************/ @@ -659,3 +699,8 @@ long String::toInt(void) const } +float String::toFloat(void) const +{ + if (buffer) return float(atof(buffer)); + return 0; +} |