How to Convert String to Float in Arduino
Using Arduino IDE functions, we can modify strings and convert them into floats. String.toFloat() function converts the input string to float values. While using this function we need to look for the string values first. String should always start with a digit number rather than alphabet. If string contains a non-digit character, the function will not work and return 0.
For example: “789.12”, “789” and “789numb” all will return “789.12”, “789.00” and “789.00” respectively. Floats only give a precise value of up to 7-8 decimals; after that its value may vary.
Syntax
Parameter Values
A variable of type String.
Return Values
A value 0 having a data type float will be returned if the string does not begin with a digit value.
Example Code
Serial.begin(9600);
String Test_String = "199.02";
Serial.print("String Before Conversion: ");
Serial.println(Test_String); // string before conversion to float
float Test_Float = Test_String.toFloat();
Serial.print("String After Conversion: ");
Serial.println(Test_Float); // string after conversion to float
}
void loop() {
}
In above code first we initialized serial communication to print output on serial monitor. Next a string is defined having value of “199.02”. To print the defined string Serial.print() function is used.
In the second part of code Test_String.toFloat() function will convert string value to float data type. Again, using serial print results are printed on the serial monitor.
Output
Serial monitor shows output of conversion from String to float data type. Both values before and after conversion are displayed.
How to Convert String to Double in Arduino
To convert string to double data type in Arduino programming String.toDouble() function is used. It converts any value taken as input in string to double. As we mentioned in the toFloat() function toDouble() also does not perform conversion when any non-digit value is used inside the string. String should always start with a digit number rather than alphabet. If a string contains non-digit characters, the function will not work and return 0.
For example: “123.12”, “123” and “123alpha” all will return “123.12”, “123.00” and “123.00” respectively. Note that Double only gives precise value of up to 7-8 decimals points after that its value may vary.
Syntax
Parameter Values
A variable of type String.
Return Values
A value 0 having data type Double will be returned if the string does not begin with digit value.
Example Code
Serial.begin(9600);
String Test_String = "199.9";
Serial.print("String Before Conversion: ");
Serial.println(Test_String); // string before conversion to double
float Test_Double = Test_String.toDouble();
Serial.print("String After Conversion: ");
Serial.println(Test_Double); // string After conversion to double
}
void loop() {
}
In above code first we initialized serial communication to print output on serial monitor. Next a string is defined having value of “199.9”. To print the defined string Serial.print() function is used.
In the second part of code Test_String.toDouble() function will convert string value to double data type. Again, using serial print results are printed on the serial monitor.
Output
Serial monitor shows output of conversion from String to double data type. Both values before and after conversion are displayed.
Conclusion
In Arduino programming we use strings to store and display sequences of characters. Sometimes we need to store the string data into some other data type like float and double for calculation. So, this article will help in converting string values to float and double using String.toFloat() and String.toDouble() functions.