From 4650c2c3d87def7e0b05f0273ba947096a935c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20P=C3=A9rez?= Date: Mon, 12 Sep 2016 08:56:02 +0200 Subject: [PATCH] WString: add `toDouble` `toFloat` internally converts into double and then truncates into a float, so why not add a method to return the double? --- hardware/arduino/avr/cores/arduino/WString.cpp | 9 +++++++-- hardware/arduino/avr/cores/arduino/WString.h | 1 + hardware/arduino/sam/cores/arduino/WString.cpp | 9 +++++++-- hardware/arduino/sam/cores/arduino/WString.h | 1 + 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/WString.cpp b/hardware/arduino/avr/cores/arduino/WString.cpp index 9975303c8..998cf22d3 100644 --- a/hardware/arduino/avr/cores/arduino/WString.cpp +++ b/hardware/arduino/avr/cores/arduino/WString.cpp @@ -740,6 +740,11 @@ long String::toInt(void) const float String::toFloat(void) const { - if (buffer) return float(atof(buffer)); - return 0; + return float(toDouble()); } + +double String::toDouble(void) const +{ + if (buffer) return atof(buffer); + return 0; +} \ No newline at end of file diff --git a/hardware/arduino/avr/cores/arduino/WString.h b/hardware/arduino/avr/cores/arduino/WString.h index de5632c12..08b8147bc 100644 --- a/hardware/arduino/avr/cores/arduino/WString.h +++ b/hardware/arduino/avr/cores/arduino/WString.h @@ -190,6 +190,7 @@ public: // parsing/conversion long toInt(void) const; float toFloat(void) const; + double toDouble(void) const; protected: char *buffer; // the actual char array diff --git a/hardware/arduino/sam/cores/arduino/WString.cpp b/hardware/arduino/sam/cores/arduino/WString.cpp index 533298225..e4efc7d4f 100644 --- a/hardware/arduino/sam/cores/arduino/WString.cpp +++ b/hardware/arduino/sam/cores/arduino/WString.cpp @@ -742,6 +742,11 @@ long String::toInt(void) const float String::toFloat(void) const { - if (buffer) return float(atof(buffer)); - return 0; + return float(toDouble()); } + +double String::toDouble(void) const +{ + if (buffer) return atof(buffer); + return 0; +} \ No newline at end of file diff --git a/hardware/arduino/sam/cores/arduino/WString.h b/hardware/arduino/sam/cores/arduino/WString.h index de5632c12..08b8147bc 100644 --- a/hardware/arduino/sam/cores/arduino/WString.h +++ b/hardware/arduino/sam/cores/arduino/WString.h @@ -190,6 +190,7 @@ public: // parsing/conversion long toInt(void) const; float toFloat(void) const; + double toDouble(void) const; protected: char *buffer; // the actual char array