Arduino Float to String Convert Tutorial

Float to String Arduino

How to Convert Float to String in Arduino Programming - Arduino is a platform that uses C ++ in its programming.

In programming, both C ++, Java, Python and others cannot be separated from value conversion.

A. Data Convert

With the conversion method in this programming, it is very helpful to make it easier for developers and users to see the required data.

For example, some cases in GPS device. GPS (Global Positioning System) is a method for determining location on the earth's surface. From this GPS we get coordinates consisting of Latitude and Longitude.

Latitude and Longitude are decimal fractional value with point. Example, If we want to display this point value to LCD , we need a way to convert the float value (domicile) into text.

There are many tutorials on the internet how to convert float to string that I got, but there are also many that I have tried without success.

In this article I will give an example of a float to string conversion program that I successfully used.

B. The easy way to convert floats to strings

Previously I also wrote how to Convert Data Type on Arduino Serial Monitor, but it can't convert float value to string.

In this article as another example, I am taking the coordinate values on google maps. The coordinates with Latitude = 10.158919 and Longitude = 96.124843.

Then I will convert the latidude and longitude values to strings and display them to the Arduino Serial Monitor, after that I want to know how long each string is.

The basic commands float to string are:
String data = String (float value, the number of digits behind the comma)

Please upload the following program to the Arduino board, it can be Arduino Uno, Arduino Mega, Pro Mini, to STM32.

float latitude = 10.158919;
float longitude = 96.124843;

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

String lat = String (latitude, 6);
String lon = String (longitude, 6);

int lengthLat = lat.length();
int lengthLon = lon.length();
delay(100); Serial.print("LATITUDE : " ); Serial.print(lat); Serial.print(" ---> String length: "); Serial.println(lengthLat);
Serial.print("LONGITUDE : "); Serial.print(lon); Serial.print(" ---> String length : "); Serial.println(lengthLon);
} void loop() { }

The results are as follows:

Hopefully the above program can help with a small part of your project. Hope it is useful.