[solved] How to convert float to char* LCD sprintf dtostrf

I need to convert a float value to cha*, so I can use sprintf to print out the result using a LCD panel.

found some examples, but they all use sprintf to convert float to char*; I believe float is not supported in arduino when using sprintf, or other way around. float with sprintf gives me a "?".

and I also found this example : dtostrf_example.ino · GitHub using dtostrf.
This example, again use sprintf(sprintfbuffer,"%f", floatvar); which doesn't work for me.

I'm sorry that you hit this problem.
The avr-gcc compiler used by Arduino does not have the floating point for sprintf included. You get question marks.
It's a matter of one or two options for the compiler, but perhaps it would use more memory.

You have to use dtostrf(). Also the classes of the Stream class (like the Serial library) can print float numbers.
To get everything in a buffer, you need another buffer and use dtostrf(), and paste that into the first buffer, for example with sprintf.

char buffer[40];
char t[10];
float x = 1.2345;

dtostrf(x, 6, 2, t);
sprintf(buffer, "And the height is %s lightyears", t);

I don't understand why you would have to use sprintf() simply to send the output to the LCD display. Use dtostrf() as Peter_n suggested and then just send the buffer to the LCD object using the print() method.

Peter_n:
I'm sorry that you hit this problem.
The avr-gcc compiler used by Arduino does not have the floating point for sprintf included. You get question marks.
It's a matter of one or two options for the compiler, but perhaps it would use more memory.

You have to use dtostrf(). Also the classes of the Stream class (like the Serial library) can print float numbers.
To get everything in a buffer, you need another buffer and use dtostrf(), and paste that into the first buffer, for example with sprintf.

char buffer[40];

char t[10];
float x = 1.2345;

dtostrf(x, 6, 2, t);
sprintf(buffer, "And the height is %s lightyears", t);

thank you. It works perfectly for me now.

 char buffer[17];
  char t[10];
  int aRead = analogRead(A0);
  float x = aRead/1024.0*5.0;

  dtostrf(x, 3, 2, t);
  sprintf(buffer, "Test %s", t);
  lcd.setCursor (0,1); 
  lcd.print (buffer);

econjack:
I don't understand why you would have to use sprintf() simply to send the output to the LCD display. Use dtostrf() as Peter_n suggested and then just send the buffer to the LCD object using the print() method.

I want to format many values using sprintf together then send it to the LCD, but can't format it when it's a float value.

You can take the float and dice it up. Take the decimal part and the whole number part.

Multiply the decimal part to your 6th decimal point (your actual precision)

Mash them back together.,.

BulldogLowell:
You can take the float and dice it up. Take the decimal part and the whole number part.

Multiply the decimal part to your 6th decimal point (your actual precision)

Mash them back together.,.

an example will be more helpful.