Full precision double to string conversion

Danny Arends Danny.Arends at gmail.com
Sat Nov 3 12:45:03 UTC 2018


On Saturday, 3 November 2018 at 12:27:19 UTC, Ecstatic Coder 
wrote:
> import std.conv;
> import std.stdio;
> void main()
> {
>     double value = -12.000123456;
>     writeln( value.sizeof );
>     writeln( value );
>     writeln( value.to!string() );
>     writeln( value.to!dstring() );
> }
>
> /*
> 8
> -12.0001
> -12.0001
> -12.0001
> */
>
> In Dart, value.toString() returns "-12.000123456".
>
> In C#, value.ToString() returns "-12.000123456".
>
> In D, value.to!string() returns "-12.0001" :(
>
> How can I convert a double value -12.000123456 to its string 
> value "-12.000123456", i.e. without loosing double-precision 
> digits ?

Specify how many digits you want with writefln:

writefln("%.8f", value);



More information about the Digitalmars-d-learn mailing list