working dtostrf

I needed a working dtostrf and since I could not find one I am contributing one with example.

---------------

#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 );
}

FloatPrintTest.ino (1.23 KB)

Update:

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

--------

/*
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

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.

When I see this line:

david_prentice:
there is no need to use the non-standard dtostrf except for compatibility with the small AVR.

what I see is...

there is no need ... for compatibility with the small AVR.

Perhaps it wasn't meant this way?

smith - thanks for posting this, exactly what I needed on my Feather M0.

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.

Hi, I'd like to follow-on with machman2823's question on use of the sketch. Is it to be added as a library file (*.h)? My project is to capture and transmit data from an Electret microphone to the receiver module which will trigger a hobby servo.

Please help, as I'm also a "newbie" level hack!

Cheers