www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How do you write reals or floats? (I have one sol here)

reply hellcatv hotmail.com writes:
I was desparately browsing through the phobos lib... and I haven't found a way
to copy a real to a string
I tried snprintf but it would often leave garbage in the string (I'm not sure
why yet...but it wouldn't work with gdc)
so I wrote my own ftoa
http://svn.dsource.org/svn/projects/deliria/turing/

also I have the problem that since it's called ftoa import won't bring it in
because when I call 
ftoa() it says "cannot call module" instead of calling the function
Jun 02 2004
parent reply "Walter" <newshound digitalmars.com> writes:
This should work:

    char[100] buffer;
    snprintf(buffer, buffer.length, "%Lg", r);

<hellcatv hotmail.com> wrote in message
news:c9llt3$bs3$1 digitaldaemon.com...
 I was desparately browsing through the phobos lib... and I haven't found a

 to copy a real to a string
 I tried snprintf but it would often leave garbage in the string (I'm not

 why yet...but it wouldn't work with gdc)
 so I wrote my own ftoa
 http://svn.dsource.org/svn/projects/deliria/turing/

 also I have the problem that since it's called ftoa import won't bring it

 because when I call
 ftoa() it says "cannot call module" instead of calling the function

Jun 04 2004
parent reply hellcatv hotmail.com writes:
that's nice but snprintf isn't even provided in dmd0.91 (at least it claims it
doesn't exist)
only sprintf
and the buffer overrun stuff that it could (and probably will not) cause is just
unfriendly.

why is there an atof and no ftoa? :-)
or at least a std.string function. either is bound to be faster than having
snprintf parse a format string then assign to a preallocated array of
undetermined length, and return that to the host program which then has to read
the whole string, determine the ending length and truncate it at that point for
the rest of the D program to use :-)


In article <c9p94o$2kb6$1 digitaldaemon.com>, Walter says...
This should work:

    char[100] buffer;
    snprintf(buffer, buffer.length, "%Lg", r);

<hellcatv hotmail.com> wrote in message
news:c9llt3$bs3$1 digitaldaemon.com...
 I was desparately browsing through the phobos lib... and I haven't found a

 to copy a real to a string
 I tried snprintf but it would often leave garbage in the string (I'm not

 why yet...but it wouldn't work with gdc)
 so I wrote my own ftoa
 http://svn.dsource.org/svn/projects/deliria/turing/

 also I have the problem that since it's called ftoa import won't bring it

 because when I call
 ftoa() it says "cannot call module" instead of calling the function


Jun 04 2004
parent "Walter" <newshound digitalmars.com> writes:
<hellcatv hotmail.com> wrote in message
news:c9pclq$2pn6$1 digitaldaemon.com...
 that's nice but snprintf isn't even provided in dmd0.91 (at least it

 doesn't exist)
 only sprintf

There's std.c.stdio._snprintf().
 and the buffer overrun stuff that it could (and probably will not) cause

 unfriendly.

I know.
 why is there an atof and no ftoa? :-)
 or at least a std.string function. either is bound to be faster than

 snprintf parse a format string then assign to a preallocated array of
 undetermined length, and return that to the host program which then has to

 the whole string, determine the ending length and truncate it at that

 the rest of the D program to use :-)

You're right. It just hasn't been done yet.
Jun 04 2004