How to Convert Float to String in Arduino
-
Convert
Float
toString
Using theString()
Function in Arduino -
Convert
Float
toString
Using theconcat()
Function in Arduino
This tutorial will discuss two methods to convert a float
into a string
. One method is to use the String()
function, and the other method is to use the concat()
function.
Convert Float
to String
Using the String()
Function in Arduino
To convert a float
into a string
using String()
, you need two parameters to pass into this function. The first one is the value of the float
you want to convert, and the second is the number of decimal places present in the float
number.
void loop() {
String stringOne = String(5.698, 3); // using a float and the decimal places
}
In the above code, 5.698
is a float
value and 3
is the number of decimal places. You can change these values according to the given float
number. Check the link for more information.
Convert Float
to String
Using the concat()
Function in Arduino
To convert a float
into a string
using concat()
first, define an empty string
and then pass the float
number as a parameter in the concat()
function. This method appends the parameter to the string
.
void loop() {
float parameter = 123.123; // floating number
String myString = ""; // empty string
myString.concat(parameter);
}
In the above code, a parameter is some number of type float
, and myString
is a variable of type String
. The concat()
function also returns a boolean, true if the operation is successful and false if unsuccessful. Check the link for more information.