Arduino Dtostrf Function

In this tutorial, we will discuss how to use the dtostrf() function to convert a variable of type double into its ASCII representation and store it as a string.

Convert double to ASCII Using dtostrf() Function

The dtostrf() function takes four input parameters.

  1. The first is a variable of type double, which we want to convert.
  2. The second is a variable of type char used to set the width of the output variable or number of digits.
  3. The third is a variable of type char used to set the number of digits after the decimal place.
  4. The fourth is a variable of type char to which the conversion will be stored. For example, check the below code.
double a = 123.123;

char x[8];

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

void loop(){
    dtostrf(a,5,2,x);
    Serial.println(x);
}

In the above code, a is a variable of type double to store the given variable, and x is a variable of type char to store the result of conversion. The result of this conversion will be 123.12.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Arduino String

  • Arduino Serial Read String