Float/double (and other any type) with sprintf()

Hello everybody,
Unfortunately I haven’t found any good solution for printing float/double values when using sprintf(). Arduino doesn’t support “%lf”, nor “%f” yet, so we need to find a different method.
Some people divide the float in two parts and print them, some are using dtostrf(). I am usually using -not necessarily better- a different approach, that might be worth sharing.
The following approaches, that I am aware of:

Using Casting*(my part)*:

  double f = 0.13454;
  Serial.print("Original value: ");
  Serial.println(f);

  char str[50];
  sprintf(str, "String value using cast: %s", String(f, 5).c_str());
  Serial.println(str);

Using part Devision*1,2*:

double f = 0.13454;
  Serial.print("Original value: ");
  Serial.println(f);

  char str[50];
  sprintf(str, "String value using devision: %d.%02d", (int)f, (int)(f * 100) % 100);
  Serial.println(str);

Using dtostrf()1**: **

double f = 0.13454;
  Serial.print("Original value: ");
  Serial.println(f);

  char str[50];
  strcpy(str, "String value using dtostrf: ");
  dtostrf(f, 2, 2, &str[strlen(str)]);
  Serial.println(str);

I found the casting approach to be the best one, as it requires the least amount of code and the precision can be set accordingly. Though it is really an ugly data type converter.
Actually literally every Data type can be printed with this solution. (bumpy but works ; ) )

Are there any other better ideas?

[1] How to sprintf a float with Arduino - Yet Another Arduino Blog

[2]https://forum.arduino.cc/index.php?topic=149575.0

Sprintf has been deliberately reduced in functionality to save space. That's why float & double are missing. There is a way to add it back in, but I don't recall where I read about it.

Your casting method is a bit dubious because you've employed String objects which are dangerous on small Arduinos.

dtostrf is what I use.

Hi,
Thanks for your response.
" dangerous on small Arduinos"
Why is that so? And how would I find out?

Read about it here: The Evils of Arduino Strings | Majenko Technologies

Strings cause memory problems and program crashes, due to poor memory management.

Use C-strings instead (zero terminated character arrays).

I rhink in his case is not important, the String is local, not static and doesn’t need memory permanently.

wildbill:
Sprintf has been deliberately reduced in functionality to save space.

For a ATmega8 !
They could have included floating point for sprintf() when Arduino turned to version 1.0.

Beside dtostrf(), there is also dtostre().