Skip to content

Commit 82a2c1d

Browse files
tevinocmaglie
authored andcommitted
add String.toFloat
1 parent 0778f8a commit 82a2c1d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

hardware/arduino/cores/arduino/WString.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ String::String(unsigned long value, unsigned char base)
100100
*this = buf;
101101
}
102102

103+
String::String(float value, int decimalPlaces)
104+
{
105+
init();
106+
char buf[33];
107+
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
108+
}
109+
110+
String::String(double value, int decimalPlaces)
111+
{
112+
init();
113+
char buf[33];
114+
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
115+
}
103116
String::~String()
104117
{
105118
free(buffer);
@@ -283,6 +296,20 @@ unsigned char String::concat(unsigned long num)
283296
return concat(buf, strlen(buf));
284297
}
285298

299+
unsigned char String::concat(float num)
300+
{
301+
char buf[20];
302+
char* string = dtostrf(num, 8, 6, buf);
303+
return concat(string, strlen(string));
304+
}
305+
306+
unsigned char String::concat(double num)
307+
{
308+
char buf[20];
309+
char* string = dtostrf(num, 8, 6, buf);
310+
return concat(string, strlen(string));
311+
}
312+
286313
/*********************************************/
287314
/* Concatenate */
288315
/*********************************************/
@@ -343,6 +370,19 @@ StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
343370
return a;
344371
}
345372

373+
StringSumHelper & operator + (const StringSumHelper &lhs, float num)
374+
{
375+
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
376+
if (!a.concat(num)) a.invalidate();
377+
return a;
378+
}
379+
380+
StringSumHelper & operator + (const StringSumHelper &lhs, double num)
381+
{
382+
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
383+
if (!a.concat(num)) a.invalidate();
384+
return a;
385+
}
346386
/*********************************************/
347387
/* Comparison */
348388
/*********************************************/
@@ -659,3 +699,8 @@ long String::toInt(void) const
659699
}
660700

661701

702+
float String::toFloat(void) const
703+
{
704+
if (buffer) return float(atof(buffer));
705+
return 0;
706+
}

hardware/arduino/cores/arduino/WString.h

+7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class String
6868
explicit String(unsigned int, unsigned char base=10);
6969
explicit String(long, unsigned char base=10);
7070
explicit String(unsigned long, unsigned char base=10);
71+
explicit String(float, int decimalPlaces=6);
72+
explicit String(double, int decimalPlaces=6);
7173
~String(void);
7274

7375
// memory management
@@ -100,6 +102,8 @@ class String
100102
unsigned char concat(unsigned int num);
101103
unsigned char concat(long num);
102104
unsigned char concat(unsigned long num);
105+
unsigned char concat(float num);
106+
unsigned char concat(double num);
103107

104108
// if there's not enough memory for the concatenated value, the string
105109
// will be left unchanged (but this isn't signalled in any way)
@@ -120,6 +124,8 @@ class String
120124
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
121125
friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
122126
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
127+
friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
128+
friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
123129

124130
// comparison (only works w/ Strings and "strings")
125131
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
@@ -172,6 +178,7 @@ class String
172178

173179
// parsing/conversion
174180
long toInt(void) const;
181+
float toFloat(void) const;
175182

176183
protected:
177184
char *buffer; // the actual char array

0 commit comments

Comments
 (0)