convert double type into string type in arduino sketch

38,499

Solution 1

Another Way To Convert Double To String:

    char TempString[10];  //  Hold The Convert Data

    dtostrf(ambientTemp,2,2,TempString);
 // dtostrf( [doubleVar] , [sizeBeforePoint] , [sizeAfterPoint] , [WhereToStoreIt] )
    YourArduinoData = String(TempString);  // cast it to string from char 

Solution 2

Use - String(val, decimalPlaces)

example,

double a = 10.2010;

String SerialData="";

SerialData = String(a,4);

Serial.println(SerialData);

Solution 3

Something like this might work:

String double2string(double n, int ndec) {
    String r = "";

    int v = n;
    r += v;     // whole number part
    r += '.';   // decimal point
    int i;
    for (i=0;i<ndec;i++) {
        // iterate through each decimal digit for 0..ndec 
        n -= v;
        n *= 10; 
        v = n;
        r += v;
    }

    return r;
}
Share:
38,499
Yang
Author by

Yang

Updated on January 04, 2020

Comments

  • Yang
    Yang over 4 years
    double ambientTemp=44.00;
    String yourdatacolumn="yourdata=";
    String yourdata; 
    double yourarduinodata=ambientTemp; 
    yourdata = yourdatacolumn + yourarduinodata; 
    

    //I want the output to be string. but because of yourarduinodata is double type. Can not convert it to string. then , I put (String) in front of yourarduinodata, still doesn't let me run throught.

    Anyone has any idea about convert double type into string type in arduino sketch;