I am trying to convert a floating number, say 68.88 into string. I will use dtostrf () . Does dtostrf() does rounding?
For example,
Assuming floating_number = 68.88
Will dtostrf(floating_number_input, 4, 1, stringBuf) return 68.9 or 68.8?
Thanks.
I am trying to convert a floating number, say 68.88 into string. I will use dtostrf () . Does dtostrf() does rounding?
For example,
Assuming floating_number = 68.88
Will dtostrf(floating_number_input, 4, 1, stringBuf) return 68.9 or 68.8?
Thanks.
Function ask many parameters.
char * dtostrf (double __val, signed char __width, unsigned char __prec, char *__s)
Atmel don't give more info:
http://www.atmel.no/webdoc/AVRLibcReferenceManual/group__avr__stdlib_1ga060c998e77fb5fc0d3168b3ce8771d42.html
I found this in german, you can ask google to translate in english:
http://andiseins.pbworks.com/w/page/54402438/dtostrf()
I think function can round the number.
Or you can round number before to call dtostrf()
I think function can round the number.
Or you can round number before to call dtostrf()
Or, you could have written a test sketch in less time than it took to post the question!
recall that not all floating point numbers can be represented exactly in IEE754 format. so
floating_number = 68.88;
has quite a fair chance that it is rounded, even before using dtostrf();
The math internally in the function has also error propagating (which you may assume is minimized) just like all floating point math, so that is the second cause for rounding errors.
Finally the function can have deliberate rounding/or not (don't know, don't have the source code).
The Arduino (Serial) print does have a rounding, which cannot be suppressed.
PaulS:
Or, you could have written a test sketch in less time than it took to post the question!
I'm with PaulS on this one.
Did you want us to write a test sketch, for you, to find out what you could have found out for yourself? If so, why?
My apologies. I wasn't using an Arduino board with a convenient access to serial port. I was using a customized board using Atmega328 with no convenient access to the outside world.
Certainly did not expect the people here to write a sketch for me. The folks here are already helpful enough. Was thinking if someone could answer the question at the top of his head without much trouble. The replies are very helpful. Thank you for your patience.
If, like me you ended up while googling this and just want a reply: yes, dtostrf rounds:
void setup() {
Serial.begin(115200);
while (!Serial) { ; }
Serial.println("Testing dtstrf");
float g=1.44;
float h=1.45;
float i=1.46;
float j=1.50;
float k=1.51;
float l=1.99;
static char outstr[8];
dtostrf(g,3,1,outstr);
Serial.print("1.44 with 1 digit: ");
Serial.println(outstr);
dtostrf(h,3,1,outstr);
Serial.print("1.45 with 1 digit: ");
Serial.println(outstr);
dtostrf(i,3,1,outstr);
Serial.print("1.46 with 1 digit: ");
Serial.println(outstr);
dtostrf(j,3,1,outstr);
Serial.print("1.50 with 1 digit: ");
Serial.println(outstr);
dtostrf(k,3,1,outstr);
Serial.print("1.51 with 1 digit: ");
Serial.println(outstr);
dtostrf(l,3,1,outstr);
Serial.print("1.99 with 1 digit: ");
Serial.println(outstr);
}
void loop() {
}
Result:
Testing dtstrf
1.44 with 1 digit: 1.4
1.45 with 1 digit: 1.5
1.46 with 1 digit: 1.5
1.50 with 1 digit: 1.5
1.51 with 1 digit: 1.5
1.99 with 1 digit: 2.0