String.toDouble()

Description

Converts a valid String to a double. The input String should start with a digit. If the String contains non-digit characters, the function will stop performing the conversion. For example, the Strings "123.45", "123", and "123fish" are converted to 123.45, 123.00, and 123.00 respectively. Note that "123.456" is approximated with 123.46. Note too that floats have only 6-7 decimal digits of precision and that longer Strings might be truncated.

Syntax

myString.toDouble()

Parameter Values

  • myString: a variable of type String.

Return Values

  • If no valid conversion could be performed because the String doesn't start with a digit, a zero is returned. Data type: double.

Example Code

void setup() { Serial.begin(9600); String myString = "20.9"; float myDouble = myString.toDouble(); Serial.println(myDouble); } void loop() { }

The result on Serial Monitor:

COM6
Send
20.90
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTES AND WARNINGS:

If the string is modified, it is highly recommended using String.reserve() to prevent the memory fragmentation issue

See Also

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES