Arduino

Arduino dtostrf() Function -Turn Your Floats into Strings

While programming Arduino boards we have to deal with different programming techniques to manipulate the data accordingly. To deal with data we need multiple functions that help us to write code for our project. One of the widely used functions is dtostrf() which converts double and float values into string with defined precision.

Arduino dtostrf() Function

Arduino dtostrf() function converts double and floating-point values into a string. Using this function, a double value is passed and converted into an ASCII representation value that will be stored inside the string. Caller is responsible to provide sufficient storage inside the string.

dtostrf() function helps while displaying a text on an LCD matrix screen using Arduino. Once the data is converted to a string it will act as a text that can be displayed anywhere it can be either an OLED screen or any other display module.

Let’s see the syntax it follows.

Syntax

Following is the syntax of dtostrf() function:

dtostrf(floatValue, minStringWidth, numAfterDecimal, charBuf_to_store_string);

Parameters

This function takes four parameters to convert double into an ASCII value stored inside string:

1. floatValue: It is the first parameter that takes the float value which we want to convert into string.

2. minStringWidth: This is the second parameter which defines the minimum field width of output string. If we set minimum string width less than the converting floating-point values, the extra digits will still display and for counting decimal and negative sign is also included as spaces. On the other hand, if the defined width for string is more than converting value the extra values will be filled with blank spaces.

3. numAfterDecimal: Third parameter is precision which describes number of digits after decimal point. It rounds off the extra digits to the number specified in precision after decimal point.

4. charBuffer: Final argument is where string will be stored. This is a kind of char array having defined size. While considering the sign make sure to consider:

  • Needs to be big enough to store value.
  • Considering size must remember space for decimal “.” and possible negative “-” sign.
  • 1 for the null terminating character “\0”.

Following is the data type these four parameters takes:

  • First parameter is a variable which is of type double.
  • Second parameter is of type char which specifies the width of the output string.
  • Third parameter is of type char which specifies the number of digits after the decimal place.
  • Fourth parameter is a variable of type char which stores converted values.

Return Data

A pointer to the converted string is returned using dtostrf() function.

Example Code

double source = 789.127; /*Source as a double variable is initialized*/

char destination[8]; /*destination array size defined*/

void setup(){

Serial.begin(9600); /*Serial Communication begins*/

dtostrf(source,5,2,destination); /*Double converted to string*/

Serial.println(destination); /*Destination string is printed*/

}

void loop(){

}

Here in above code a double point source variable is defined having value of “789.127” next a destination buffer array is initialized with a size of “8”. In the setup part using dtostrf() function source, the double point value will be converted to a string having minimum width of 5 and decimal precision of 2 digits after decimal place. At the end of the code, we printed the result on the serial monitor.

Output

Output terminal shows us a string which is rounded off to 2 decimal points.

Conclusion

To convert double value into a string an Arduino AVR programming function is used known as dtostrf(). This function takes four parameters: a source which is a floating-point value that will be converted to a destination string having defined minimum width value and for precision a parameter is defined that round off to specific decimal values. This article will help to understand all these in a better way.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.