jimLee
February 2, 2017, 6:30pm
1
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
jimLee
February 2, 2017, 7:02pm
5
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
jimLee
February 2, 2017, 7:06pm
6
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:
/* Copyright (c) 2005, Dmitry Xmelkov
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of the copyright holders nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
This file has been truncated. show original
And to save you the trouble:
/* Copyright (c) 2005, Dmitry Xmelkov
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of the copyright holders nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
This file has been truncated. show original
I see a lot of while(width)'s in there!
jimLee
February 2, 2017, 7:53pm
8
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".
tf68
February 3, 2017, 2:32am
12
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.