How to convert double to string in arduino

Hello, all

I am trying to use Gobetwino and it is important to convert double to string. When i try to convert integer to string by using function "itoa" it works. But when i used function "gcvt" from C++ to convert double to string. Arduino show error message as

" TEMPFIX.cpp: In function 'void logData(double)': TEMPFIX:281: error: 'gcvt' was not declared in this scope"

the following is some part of the overall code:

void logData( double result) 
{
   char buffer[10];
   int ndigit=2; 
  
   Serial.print("#S|TEMPFIX|[");
   Serial.print(gcvt((result),ndigit, buffer));
   Serial.print(176,BYTE);
   Serial.println("]#");
   readSerialString(serInString,1000);

}

please help

Thanks

You can print a float/double directly and specify the number of fractional digits:

void logData( double result) 
{
   int ndigit = 2; 
  
   Serial.print("#S|TEMPFIX|[");
   Serial.print(result,ndigit));
   Serial.print(176,BYTE);
   Serial.println("]#");
   readSerialString(serInString,1000);
}

Thank you for helping but it didn't worked, i tried ur code already. Do u have other suggestions??

only integer came, no decimal T T

strange it does not work

try a simpler version

void logData() 
{
   int ndigit = 2; 
  double result = 12345.678;

   Serial.print("#S|TEMPFIX|[");
   Serial.print(result,ndigit));
   Serial.print(176,BYTE);
   Serial.println("]#");
   readSerialString(serInString,1000);
}

Hello guys,

have you tried to use the sprintf function ?

void logData(double result) 
{
  // where result = 0.12345

  char format_result_end[] = "]#";
  char format_result_start[] = "#S|TEMPFIX|[";
  char buffer_result[sizeof(result)+sizeof(format_result_start)+sizeof(format_result_end)];

  sprintf(buffer_result, "%s%.3lf]#", format_result_start, result, format_result_end);
  // the 3 in .3lf can be ajusted as the numbers you want after zero.

  Serial.print(buffer_result);
  readSerialString(serInString,1000);
}

a can't test it now and its possibly don't work

edit: i forget the code tag...

1 Like

I don't know if this will help you, but I had a sort of similar problem trying to display a Voltage reading from an ADC on an LCD screen.

I tried viewing serial output but all I could see on either the screen or the serial monitor was a "?" instead of any values. Apparently sprintf does not like to work with floating point numbers sometimes but I don't know why...

Anyway, what I did was take the result of my ADC conversion and calibration (float), multiply it by 1000 to bring the important numbers to the left of the decimal point, then convert the float to an integer (which truncates everything after the decimal point), and then use sprintf to convert the integer into a char string, and then finally use sprintf again to display the correct number. So the relevant parts of my code are:

sensorValue = analogRead(battPin); // ADC read
fBattVoltage = (((float)sensorValue/1024.0f)*1.1f)/(0.124f)*1000.0f;  // ADC calibration
iBattVoltage = (int)fBattVoltage; // Conversion of float to integer
char battVoltageStr[50]; //Assignment of char
sprintf(battVoltageStr,"%d",iBattVoltage); // Copying integer to char as string
sprintf(pinA1,"%c.%c%c V   ",battVoltageStr[0],battVoltageStr[1],battVoltageStr[2]); // Using indexed arguments to put each number in the right place - notice the manually inserted decimal point.

The result of that code is something like "7.98 V"

I realize it's not your exact problem, and also my code is probably inefficient and roundabout...but it was the only way I could get a decimal number to show on my display. Hopefully this somehow helps you or sparks an idea at least!

1 Like

Thx to u all , i can fix the code already.

Thx u very very very much ^^ finally!!!!

Apparently sprintf does not like to work with floating point numbers sometimes but I don't know why...

Sometimes is incorrect. Never would be correct. The reason is that floating point to string conversion takes up a lot of code space.

I see...I'm confused then, why have the "f" specifier at all?

I see...I'm confused then, why have the "f" specifier at all?

The "f" specifier is part of the language. It can not just be removed from the language. It does not necessarily have to be implemented in the expected way, however.

I've seen it mentioned specifically in documentation for "sprintf," hence the confusion. Then again it's the internet and you can't trust everything...

Syntax

String(val) String(val, base) String(val, decimalPlaces)

Parameters

val: a variable to format as a String - string, char, byte, int, long, unsigned int, unsigned long, float, double base (optional) - the base in which to format an integral value decimalPlaces (only if val is float or double) - the desired decimal places

Returns

an instance of the String class