float to string ?

Hello I work with a "STRING" problem.
I need to send some data to my server which will be packaged together in a string with ID data and link file name.
I've simplified the code to the problem.

If I use "int testc = 25" so it works although without decimals.
I must, however, use decimals to a calculation.
but if I use "float testc = 25.65".
Become the result nonsense.

I think that my problem has something to do with %c to do.
but I'm not sure.

#include <SPI.h>

char * dst;
char data[0xff];
float testc=25.65;


void setup()
{
  Serial.begin(9600);

}

void loop()
{
  Serial.println(testc);// Test out put.

  strcpy(data,"/test_fil_1_ok_3.php?id=test444&temp=");
  dst = data + strlen(data);
  dst += sprintf(dst, "%c", testc);
  Serial.println(data);
  delay (10000);

  
}

Does anyone have an idea.
best Kim Jessen

%c specifies to print the data as an ASCII char. If you want to print as a floating point value, use %f.

Regards,
Ray L.

Except that there isn't support for %f in the arduino. The easiest way is to convert the float to a string first with dtostrf and then use sprintf with that string already as a string.

Delta_G:
Except that there isn't support for %f in the arduino. The easiest way is to convert the float to a string first with dtostrf and then use sprintf with that string already as a string.

Didn't know that - never used Arduino printf/sprintf. Yet another way to do it is:

dst += sprintf(dst, "%d.%d", (int)testc, (int)((testc - (int)testc) * 1000.0));

This also lets you specify the number of decimal points that get printed.

Regards,
Ray L.

Try:

dtostrf(float floatvar, int StringLengthIncDecimalPoint, int numVarsAfterDecimal, char charbuf[]);

RayLivingston:
Didn't know that - never used Arduino printf/sprintf. Yet another way to do it is:

dst += sprintf(dst, "%d.%d", (int)testc, (int)((testc - (int)testc) * 1000.0));

This also lets you specify the number of decimal points that get printed.

...but suffers from leading zero suppression in the fraction.

hey Ray L,
It works many thanks for the help

kimjessen:
hey Ray L,
It works many thanks for the help

Try printing 1.5 and 1.05

AWOL:
...but suffers from leading zero suppression in the fraction.

OK, then:

dst += sprintf(dst, "%d.%03d", (int)testc, (int)((testc - (int)testc) * 1000.0));

Or does Arduino sprintf also not support length specifiers?

Regards,
Ray L.

RayLivingston:
does Arduino sprintf also not support length specifiers?

It does, I'v tried :stuck_out_tongue: