char * dtostrf( float const val, int8_t const width, uint8_t const prec, char * const s, bool const trim )
{
dtostrf( val, width, prec, s );
if ( prec > 0 && trim == true )
{
// pointer to start of string
char * p1 = s;
// move to end of string
while ( *++p1 );
// if left-justified
if ( width < 0 )
{
// move back to first non-space character
while ( *--p1 == ' ' );
// move back to first non-zero character, replacing zero(s) by space(s)
while ( *p1 == '0' ) { *p1 = ' '; --p1; }
// if it's the dot, replace it by space
if ( *p1 == '.' ) *p1 = ' ';
}
// else if right-justified
else
{
// pointer to last character
char * p2 = --p1;
// move back to first non-zero character
while ( *p2 == '0' ) --p2;
// if it's the dot, ignore it
if ( *p2 == '.' ) --p2;
// if position changed
if ( p2 != p1 )
{
// move characters to new position
while ( p2 >= s && *p2 != ' ' ) { *p1 = *p2; --p1; --p2; }
// erase old characters
while ( p1 >= s && *p1 != ' ' ) { *p1 = ' '; --p1; }
}
}
}
return s;
}
void print( const float f )
{
char s[10];
Serial.print( "\"" );
Serial.print( f, 3 );
Serial.print( "\" \"" );
Serial.print( dtostrf( f, -7, 3, s ) );
Serial.print( "\" \"" );
Serial.print( dtostrf( f, 7, 3, s ) );
Serial.print( "\" \"" );
Serial.print( dtostrf( f, -7, 3, s, true ) );
Serial.print( "\" \"" );
Serial.print( dtostrf( f, 7, 3, s, true ) );
Serial.println( "\"" );
}
void setup()
{
Serial.begin( 921600 );
for ( float f = -1; f <= 0; f += 0.025 )
{
print( f );
}
for ( float f = 0; f < 1; f += 0.025 )
{
print( f );
}
for ( float f = 10; f < 11; f += 0.025 )
{
print( f );
}
}
void loop()
{
}