dtostrf what does the width do, in my example?

Hi,

Have been trying to work this out for last hour and a half ?

I can trim the result after the decimal point, as in this example will output 1234.8

But Width part of dtostrf has no effect, (in this example it set to 2) what does it do ? as it seems to be to small for the whole number including decimal point , but it prints out correctly?

Thanks

Regards

Gary

char  buffer[12];  
float number = 1234.84;
     
dtostrf(number,2,1,buffer);  // dtostrf=(x,W,P,x) Width= Characters including dot & minus sign. Precision= Characters after decimal point.     
    
     Serial.println("");
     Serial.print(F("buffer = "));
     Serial.println(buffer);

Width is the minimum number of characters in the output. If the value takes more than that, width will be ignored.

Make the array size 20 characters, and change width to 12, and see what happens.

When you print the value, print it inside markers, like so:

     Serial.print(F("buffer = ["));
     Serial.print(buffer);
     Serial.println("]");

Change the width, and see what happens.

Hi PaulS,

Thanks for that, I now understand how to use it.

regards

gary