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"
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);
}
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!
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...
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