Go Down

### Topic: working dtostrf (Read 7044 times)previous topic - next topic

#### jsmith

##### Dec 30, 2015, 06:46 am
I needed a *working* dtostrf and since I could not find one I am contributing one with example.

-------<SNIP>--------

#include <stdlib.h>

float x = 0.0;
float y = 0.0;

char *dtostrf(double value, int width, unsigned int precision, char *result)
{
int decpt, sign, reqd, pad;
const char *s, *e;
char *p;
s = fcvt(value, precision, &decpt, &sign);
if (precision == 0 && decpt == 0) {
s = (*s < '5') ? "0" : "1";
reqd = 1;
} else {
reqd = strlen(s);
if (reqd > decpt) reqd++;
if (decpt == 0) reqd++;
}
if (sign) reqd++;
p = result;
e = p + reqd;
pad = width - reqd;
if (pad > 0) {
e += pad;
while (pad-- > 0) *p++ = ' ';
}
if (sign) *p++ = '-';
if (decpt <= 0 && precision > 0) {
*p++ = '0';
*p++ = '.';
e++;
while ( decpt < 0 ) {
decpt++;
*p++ = '0';
}
}
while (p < e) {
*p++ = *s++;
if (p == e) break;
if (--decpt == 0) *p++ = '.';
}
if (width < 0) {
pad = (reqd + width) * -1;
while (pad-- > 0) *p++ = ' ';
}
*p = 0;
return result;
}

void setup() {
while( !SerialUSB ) ;
Serial.begin(9600);
}

void loop() {
char str1[20];
char str2[20];
x += 0.001;
y -= 0.001;
dtostrf(x, 6, 3, str1);
dtostrf(y, 6, 3, str2);
SerialUSB.print( "X Val:" );
SerialUSB.print( str1 );
SerialUSB.print( " Y Val:" );
SerialUSB.println( str2 );
}

#### jsmith

#1
##### Dec 30, 2015, 08:35 pm
Update:

Edit your dtostrf.c to look like the following. It fixes LOTS of bugs in the current libraries.

----<SNIP>----

/*
dtostrf - Emulation for dtostrf function from avr-libc
Copyright (c) 2015 Arduino LLC.  All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdio.h>

#if 0
char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
char fmt[20];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(sout, fmt, val);
return sout;
}
#else
#include <string.h>
#include <stdlib.h>
char *dtostrf(double val, int width, unsigned int prec, char *sout)
{
int decpt, sign, reqd, pad;
const char *s, *e;
char *p;
s = fcvt(val, prec, &decpt, &sign);
if (prec == 0 && decpt == 0) {
s = (*s < '5') ? "0" : "1";
reqd = 1;
} else {
reqd = strlen(s);
if (reqd > decpt) reqd++;
if (decpt == 0) reqd++;
}
if (sign) reqd++;
p = sout;
e = p + reqd;
pad = width - reqd;
if (pad > 0) {
e += pad;
while (pad-- > 0) *p++ = ' ';
}
if (sign) *p++ = '-';
if (decpt <= 0 && prec > 0) {
*p++ = '0';
*p++ = '.';
e++;
while ( decpt < 0 ) {
decpt++;
*p++ = '0';
}
}
while (p < e) {
*p++ = *s++;
if (p == e) break;
if (--decpt == 0) *p++ = '.';
}
if (width < 0) {
pad = (reqd + width) * -1;
while (pad-- > 0) *p++ = ' ';
}
*p = 0;
return sout;
}
#endif

#### david_prentice

#2
##### Dec 31, 2015, 09:49 am
Please edit your code so that it lies within code tags.

Note that SAM code links with full-fat printf() family.    So there is no need to use the non-standard dtostrf except for compatibility with the small AVR.

David.

#### pjrc

#3
##### Jan 01, 2016, 07:53 pm
When I see this line:

there is no need to use the non-standard dtostrf except for compatibility with the small AVR.
what I see is...

Quote
there is no need ... for compatibility with the small AVR.
Perhaps it wasn't meant this way?

#### roadfun

#4
##### Mar 02, 2016, 08:06 pm
smith - thanks for posting this, exactly what I needed on my Feather M0.

#### machman2823

#5
##### Dec 19, 2017, 07:58 pm
I'm wondering how to use this in a sketch. Do I have to make it into a library? if so, how would I do that?

It would be used to display temperature and humidity data with a feather M0 and Adafruit's quad alphanumeric display wing. any help would be great.

Go Up