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 1/2] 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 9975303c8e1..998cf22d38f 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 de5632c12c1..08b8147bcfe 100644 --- a/hardware/arduino/avr/cores/arduino/WString.h +++ b/hardware/arduino/avr/cores/arduino/WString.h @@ -190,6 +190,7 @@ class String // 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 533298225e4..e4efc7d4f55 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 de5632c12c1..08b8147bcfe 100644 --- a/hardware/arduino/sam/cores/arduino/WString.h +++ b/hardware/arduino/sam/cores/arduino/WString.h @@ -190,6 +190,7 @@ class String // parsing/conversion long toInt(void) const; float toFloat(void) const; + double toDouble(void) const; protected: char *buffer; // the actual char array From 24cba16802a30923425af037ceeab44bfd4cfca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20P=C3=A9rez?= Date: Mon, 12 Sep 2016 09:00:04 +0200 Subject: [PATCH 2/2] Spaces to tab --- hardware/arduino/avr/cores/arduino/WString.cpp | 6 +++--- hardware/arduino/avr/cores/arduino/WString.h | 2 +- hardware/arduino/sam/cores/arduino/WString.cpp | 6 +++--- hardware/arduino/sam/cores/arduino/WString.h | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/WString.cpp b/hardware/arduino/avr/cores/arduino/WString.cpp index 998cf22d38f..f2572d60883 100644 --- a/hardware/arduino/avr/cores/arduino/WString.cpp +++ b/hardware/arduino/avr/cores/arduino/WString.cpp @@ -745,6 +745,6 @@ float String::toFloat(void) const double String::toDouble(void) const { - if (buffer) return atof(buffer); - return 0; -} \ No newline at end of file + if (buffer) return atof(buffer); + return 0; +} diff --git a/hardware/arduino/avr/cores/arduino/WString.h b/hardware/arduino/avr/cores/arduino/WString.h index 08b8147bcfe..77709c3ba42 100644 --- a/hardware/arduino/avr/cores/arduino/WString.h +++ b/hardware/arduino/avr/cores/arduino/WString.h @@ -190,7 +190,7 @@ class String // parsing/conversion long toInt(void) const; float toFloat(void) const; - double toDouble(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 e4efc7d4f55..71bbc07d13b 100644 --- a/hardware/arduino/sam/cores/arduino/WString.cpp +++ b/hardware/arduino/sam/cores/arduino/WString.cpp @@ -747,6 +747,6 @@ float String::toFloat(void) const double String::toDouble(void) const { - if (buffer) return atof(buffer); - return 0; -} \ No newline at end of file + if (buffer) return atof(buffer); + return 0; +} diff --git a/hardware/arduino/sam/cores/arduino/WString.h b/hardware/arduino/sam/cores/arduino/WString.h index 08b8147bcfe..77709c3ba42 100644 --- a/hardware/arduino/sam/cores/arduino/WString.h +++ b/hardware/arduino/sam/cores/arduino/WString.h @@ -190,7 +190,7 @@ class String // parsing/conversion long toInt(void) const; float toFloat(void) const; - double toDouble(void) const; + double toDouble(void) const; protected: char *buffer; // the actual char array