The largest value I would expect to get this way:99.99
this is a misunderstanding or you did not read the right reference.
the doc states:
char* dtostrf ( double __val,
signed char __width,
unsigned char __prec,
char * __s
)
The dtostrf() function converts the double value passed in val into an ASCII representationthat will be stored under s. The caller is responsible for providing sufficient storage in s.
Conversion is done in the format "[-]d.ddd". The **minimum** field width of the output string (including the possible '.' and the possible sign for negative values) is given in width, and prec determines the number of digits after the decimal sign. width is signed value, negative for left adjustment.
The dtostrf() function returns the pointer to the converted string s.
so the width value is to be seen as a minimum. There is no action taken on your float, It's up to you to ensure that there is enough space in the buffer by constraining your float to what will fit.
The idea of the minimum width is that if you right adjust then you can pad with space on the left and always get a cString that "looks good" and is properly aligned in a column. If you left adjust then padding is added AFTER the conversion in ASCII so that if you print this on a LCD for example, you always know how many characters you have (and erase them) or just have nice looking left aligned columns
try this code
const byte bufferSize = 20;
char tmpBuffer[bufferSize];
void dumpTmpBuffer()
{
for (byte i = 0; i < bufferSize; i++) {
if (tmpBuffer[i] == '\0') Serial.write('*'); // a '*' will denote an NULL character
else if (tmpBuffer[i] == ' ') Serial.write('_'); // an underscore will show a space is there
else Serial.print(tmpBuffer[i]); // otherwise we print the character
Serial.write(' '); // and add a spacer for readability
}
Serial.println();
}
void setup() {
Serial.begin(115200); // Serial console set at 115200 Bauds
double d1 = -10.345;
memset(tmpBuffer, '\0', bufferSize); // fill up the buffer with '\0'
dtostrf(d1, 10, 3, tmpBuffer); // right align
dumpTmpBuffer();
memset(tmpBuffer, '\0', bufferSize); // fill up the buffer with '\0'
dtostrf(d1, -10, 3, tmpBuffer); // left align
dumpTmpBuffer();
memset(tmpBuffer, '\0', bufferSize); // fill up the buffer with '\0'
dtostrf(d1, 4, 3, tmpBuffer); // right align, not enough space
dumpTmpBuffer();
memset(tmpBuffer, '\0', bufferSize); // fill up the buffer with '\0'
dtostrf(d1, -4, 3, tmpBuffer); // right align, not enough space
dumpTmpBuffer();
}
void loop() {}
Serial Monitor (@ 115200 bauds) will show
[color=purple]
_ _ _ - 1 0 . 3 4 5 * * * * * * * * * *
- 1 0 . 3 4 5 _ _ _ * * * * * * * * * *
- 1 0 . 3 4 5 * * * * * * * * * * * * *
- 1 0 . 3 4 5 * * * * * * * * * * * * *
[/color]
the stars are where null chars were kept and you can see with _ how alignment works.
in the first example you have spaces before the ASCII representation
in the second example you have spaces after the ASCII representation
with the two last examples though you can see that when the minimum width is not enough you still get the full value in the buffer, no padding as there was no room left, and would lead to overflow if you are not careful.