1
votes

I am trying to convert a string to a double in one of my Arduino projects (specifically using a Teensy 3.5 in the Arduino IDE) using Arduino's string.toDouble() command. When I try to implement the function as shown in the code below, I get the error:

<'class String' has no member named 'toDouble'>.

However, string.toFloat() and string.toInt() work successfully.

Any ideas as to what is going wrong?

String myNumberString = "100";
double myNumber = 0;

void setup() {
  Serial.begin(9600);  
}

void loop() {
  myNumber = myNumberString.toDouble()+1;
  Serial.println(myNumber);
  myNumberString = String(myNumber);
  delay(1000);
}
2

2 Answers

1
votes

The problem you are having is that arduino declares myNumberString as a String Object, so you can't use toDouble() to convert the string into a double because the function is not defined in the String class. You will have to use toFloat to convert your string. Here's the link I used to find this.

1
votes

Seem like the Teensy's Arduino core is missing that function.

I only see toInt and toFloat inside Teensy's implementation of the String class. While the original Arduino core has it implemented.

Maybe you can use atof directly, like:

myNumber = atof(myNumberString.c_str());