How to Convert Float to String in Arduino

Ammar Ali Feb 02, 2024
  1. Convert Float to String Using the String() Function in Arduino
  2. Convert Float to String Using the concat() Function in Arduino
How to Convert Float to String 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.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - Arduino String