Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert double type into string type in arduino sketch

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?

like image 396
Yang Avatar asked Nov 08 '13 19:11

Yang


3 Answers

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 
like image 88
Dani Bresler Avatar answered Jan 03 '23 16:01

Dani Bresler


Use - String(val, decimalPlaces)

example,

double a = 10.2010;

String SerialData="";

SerialData = String(a,4);

Serial.println(SerialData);
like image 35
Abhishek Pachlegaonkar Avatar answered Jan 03 '23 16:01

Abhishek Pachlegaonkar


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;
}
like image 20
Octopus Avatar answered Jan 03 '23 15:01

Octopus