Skip to content

Commit

Permalink
add String.toFloat
Browse files Browse the repository at this point in the history
  • Loading branch information
tevino authored and cmaglie committed Jun 6, 2013
1 parent 0778f8a commit 82a2c1d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
45 changes: 45 additions & 0 deletions hardware/arduino/cores/arduino/WString.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
/*********************************************/
Expand Down Expand Up @@ -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 */
/*********************************************/
Expand Down Expand Up @@ -659,3 +699,8 @@ long String::toInt(void) const
}


float String::toFloat(void) const
{
if (buffer) return float(atof(buffer));
return 0;
}
7 changes: 7 additions & 0 deletions hardware/arduino/cores/arduino/WString.h
Expand Up @@ -68,6 +68,8 @@ class String
explicit String(unsigned int, unsigned char base=10);
explicit String(long, unsigned char base=10);
explicit String(unsigned long, unsigned char base=10);
explicit String(float, int decimalPlaces=6);
explicit String(double, int decimalPlaces=6);
~String(void);

// memory management
Expand Down Expand Up @@ -100,6 +102,8 @@ class String
unsigned char concat(unsigned int num);
unsigned char concat(long num);
unsigned char concat(unsigned long num);
unsigned char concat(float num);
unsigned char concat(double num);

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

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

// parsing/conversion
long toInt(void) const;
float toFloat(void) const;

protected:
char *buffer; // the actual char array
Expand Down

0 comments on commit 82a2c1d

Please sign in to comment.