Full precision double to string conversion

Ecstatic Coder ecstatic.coder at gmail.com
Sat Nov 3 13:20:22 UTC 2018


On Saturday, 3 November 2018 at 12:45:03 UTC, Danny Arends wrote:
> 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);

Actually, what I need is the D equivalent of the default 
ToString() function we have in Dart and C#.

I mean a dumb double-to-string standard library conversion 
function which returns a string including all the double 
precision digits stored in the 52 significant bits of the value, 
preferably with the trailing zeroes removed.

For an unknown reason, D's default double-to-string conversion 
function only expose the single-precision significant digits :(



More information about the Digitalmars-d-learn mailing list