How to safely and reasonably convert a float or double to string or char array?

Then your dtostrf is going to mess the whole memory. Put it in a expensive project with mechanical stuff involved

Evidently, you need to learn some things about input conditioning and error handling, which all of us working in the real world have to deal with on a day to day basis.

It is trivial to fix or work around the limitation of dtostrf.

  1. Use snprintf() with floating point extensions enabled.

  2. Work around the limitation of dtostrf() by thinking about the problem for 10 seconds.

For example:

void setup()
{
  Serial.begin(9600);
  Serial.println("begin");
  double numberInput = 99.0;
  fp_print(numberInput);
  numberInput=999999.0;
  fp_print(numberInput);
  numberInput=10.*numberInput;
  fp_print(numberInput);
}
void loop(){}

void fp_print(float x)  //function to print a float, within range
{ 
  char buf[10];
  if ( fabs(x)<9.99999E6 ) Serial.println(dtostrf(x, 5, 2, buf));
  else Serial.println("overflow");
}