Full precision double to string conversion

Ecstatic Coder ecstatic.coder at gmail.com
Sat Nov 3 17:26:19 UTC 2018


>> Actually, what I need is the D equivalent of the default 
>> ToString() function we have in Dart and C#.
>
> I don't think it means what you think it means:
>
> void main() {
>     double value = -12.000123456;
>     int precision = 50;
>
>     import std.stdio;
>     writefln("%.*g", precision, value);
>
>     import std.format;
>     string str = format("%.*g", precision, value);
>     writeln(str);
> }
>
> Prints:
>
> -12.000123456000000743415512260980904102325439453125
> -12.000123456000000743415512260980904102325439453125
>
> That's not quite the -12.000123456 that you'd get from C#'s 
> ToString().

Unfortunately, but that's still better though, thanks :)

> All of them? Most implementations of conversion algorithms 
> actually stop when it's "good enough". AFAIR, D doesn't even 
> have it's own implementation and forwards to C, unless that 
> changed in recent years.

What I meant was that getting too many significant digits would 
still be a better solution than not having them.

But indeed what I really need is a D function which gives a 
better decimal approximation to the provided double constant, 
exactly in the same way those in Dart and C# do.

Is there really no such function in D ?





More information about the Digitalmars-d-learn mailing list