void setup() {
Serial.begin(9600);
while (!Serial) {};
}
void loop() {
float U = 1992345.45;
Serial.println(FloatToChar (U, 8));
delay(500);
}
char* FloatToChar(float U, uint8_t R ) {
// R = sign + digits
if (R < 1) return 0;
static char strf[20];
static char unit[20];
R--;
char c;
// 32 = ' '
// 43 = '+'
// 45 = '-'
// 48 = '0'
if (U < 0) c = 45;
else if (U > 0) c = 43;
else c = 32;
U = fabs(U);
if (U < pow(10, R - 2)) {
dtostrf(U, R, R - 2, strf);
} else {
dtostrf(U, R, 0, strf);
if (strf[0] == 32) strf[0] = 48;
}
snprintf(unit, R + 2, "%c%s", c, strf);
return unit;
}