0

I am using PIC18F2550. Programming it with C18 language.
I need a function that converts double to string like below:

void dtoa(  char *szString,             // Output string
            double dbDouble,            // Input number
            unsigned char ucFPlaces)    // Number of digits in the resulting fractional part
{
    // ??????????????
}

To be called like this in the main program:

void main (void)
{
    // ...
    double dbNumber = 123.45678;
    char szText[9];
    dtoa(szText, dbNumber, 3); // szText becomes "123.456" or rounded to "123.457"
    // ...
}
hkBattousai
  • 10,583
  • 18
  • 76
  • 124

3 Answers3

1

So write one!

5mins, a bit of graph paper and a coffee is all it should take.
In fact it's a good interview question

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
1

Tiny printf might work for you: http://www.sparetimelabs.com/tinyprintf/index.html

Throwback1986
  • 5,887
  • 1
  • 30
  • 22
1

Generally, the Newlib C library (BSD license, from RedHat, part of Cygwin as well as used in many many "bare-metal" embedded-systems compilers) is a good place to start for usefuls sources for things that would be in the standard C library.

The Newlib dtoa.c sources are in the src/newlib/libc/stdlib subdirectory of the source tree:

The file is going to be a little odd, in that Newlib uses some odd macros for the function declarations, but should be straightforward to adapt -- and, being BSD-licensed, you can pretty much do whatever you want with it if you keep the copyright notice on it.

Brooks Moses
  • 9,267
  • 2
  • 33
  • 57