Arduino dtostrf Function

Arduino dtostrf Function

Ammar Ali Apr-29, 2021 Mar-27, 2021 Arduino Arduino Double Arduino String

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, see 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.

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

  • Arduino strcmp Function
  • Arduino strcpy Function
  • Concatenate Strings in Arduino
  • Parse a String in Arduino
  • Split String in Arduino
  • Compare Strings in Arduino