dtostrf not acting like I'd expect.

My code in code tags..

double aFloat;
char textBuff[20];

void setup() {
  Serial.begin(9600);while(!Serial);
  aFloat = 0.0000706;
  Serial.print("The float: ");Serial.println(aFloat,10);
  dtostrf(aFloat,0,0,textBuff);
  Serial.print("Result fix 0: ");Serial.println(textBuff);
  dtostrf(aFloat,0,1,textBuff);
  Serial.print("Result fix 1: ");Serial.println(textBuff);
   dtostrf(aFloat,0,1,textBuff);
  Serial.print("Result fix 2: ");Serial.println(textBuff);
}

void loop() {
  // put your main code here, to run repeatedly:

}

The output..

The float: 0.0000706000
Result fix 0: 7
Result fix 1: 0.0000
Result fix 2: 0.0000

I was expecting..
0
0.0
0.00

P.S. double or float gave the same results.

Anyone enlighten me?

Thanks.

-jim lee

The second argument is the width specifier, or the width of the output string. I am not sure what they mean by "minimum field width" but I assume it is like printf() where that is the number of characters it will print. What do you want it to do when you tell it the width is 0?

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.

I do not confirm your output.

There is an error in that dstorf for fix2 is a dupicate of fix1, but when I change it to

dtostrf(aFloat,0,2,textBuff);
  Serial.print("Result fix 2: ");Serial.println(textBuff);

I get this for output as you expected

The float: 0.0000706000
Result fix 0: 0
Result fix 1: 0.0
Result fix 2: 0.00

I see the expected results.

double aFloat;
char textBuff[20];

void setup() {
  Serial.begin(250000);
  aFloat = 0.0000706;
  Serial.print("The float: ");
  Serial.println(aFloat, 10);
  dtostrf(aFloat, 0, 0, textBuff);
  Serial.print("Result fix 0: ");
  Serial.println(textBuff);
  dtostrf(aFloat, 0, 1, textBuff);
  Serial.print("Result fix 1: ");
  Serial.println(textBuff);
  dtostrf(aFloat, 0, 1, textBuff);
  Serial.print("Result fix 2: ");
  Serial.println(textBuff);
}

void loop() {}
The float: 0.0000706000
Result fix 0: 0
Result fix 1: 0.0
Result fix 2: 0.0

Well, as far as I can figure out from the docs. The width is for padding spaces to fit into left or right justified fixed width fields. I take care of the padding myself. All I want is the number formatted with the specified decimal places with no padding.

-jim lee

Hmm.. interesting. I'm running this on a Teensy. Possibly its a special from them?

Thanks

-jim lee

Assuming you are on an AVR, feel free to dive into the docs:

And to save you the trouble:

I see a lot of while(width)'s in there!

Yeah, there are a few, but it looks like all they are doing is adding padding to the string.

Thanks for the links. I had no clue where/how to find them.

-jim lee

I just googled "dtostrf() source".

jimLee:
I'm running this on a Teensy.

Which one?

Teensy 3.2

-jim lee

The Teensy 3.x are ARM chips and do not use avrlibc. The (avrlibc) nonstandard c functions are provided in the Teensy 3.x core.