Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions cores/arduino/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
*/

#include "WString.h"
#include <float.h>

/*********************************************/
/* Static Member Initialisation */
/*********************************************/

size_t const String::FLT_MAX_DECIMAL_PLACES;
size_t const String::DBL_MAX_DECIMAL_PLACES;

/*********************************************/
/* Constructors */
Expand Down Expand Up @@ -107,15 +115,19 @@ String::String(unsigned long value, unsigned char base)

String::String(float value, unsigned char decimalPlaces)
{
static size_t const FLOAT_BUF_SIZE = FLT_MAX_10_EXP + FLT_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */;
init();
char buf[33];
char buf[FLOAT_BUF_SIZE];
decimalPlaces = decimalPlaces < FLT_MAX_DECIMAL_PLACES ? decimalPlaces : FLT_MAX_DECIMAL_PLACES;
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
}

String::String(double value, unsigned char decimalPlaces)
{
static size_t const DOUBLE_BUF_SIZE = DBL_MAX_10_EXP + DBL_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */;
init();
char buf[33];
char buf[DOUBLE_BUF_SIZE];
decimalPlaces = decimalPlaces < DBL_MAX_DECIMAL_PLACES ? decimalPlaces : DBL_MAX_DECIMAL_PLACES;
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
}

Expand Down
3 changes: 3 additions & 0 deletions cores/arduino/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class String
typedef void (String::*StringIfHelperType)() const;
void StringIfHelper() const {}

static size_t const FLT_MAX_DECIMAL_PLACES = 10;
static size_t const DBL_MAX_DECIMAL_PLACES = FLT_MAX_DECIMAL_PLACES;

public:
// constructors
// creates a copy of the initial value.
Expand Down
Loading