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.
Following is the syntax of dtostrf() function:
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.
A pointer to the converted string is returned using dtostrf() function.
char destination; /*destination array size defined*/
Serial.begin(9600); /*Serial Communication begins*/
dtostrf(source,5,2,destination); /*Double converted to string*/
Serial.println(destination); /*Destination string is printed*/
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 terminal shows us a string which is rounded off to 2 decimal points.
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.