problem with std.variant rounding

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 28 09:42:28 PDT 2017


On Friday, 28 April 2017 at 16:24:55 UTC, Suliman wrote:
> On Friday, 28 April 2017 at 15:45:25 UTC, Suliman wrote:
>> I am using https://github.com/mysql-d/mysql-native
>> It's return from DB variant data-type.
>>
>> My DB include value: 56.051151 (double type in DB)
>>
>> I need to extract it. I tried several variants:
>>
>> writeln(point[3].coerce!float);
>> writeln(point[3].coerce!string);
>> writeln(point[3].coerce!double);
>>
>> but all of them return me it as: 56.0512
>>
>> How to return exactly 56.051151 ?
>
> import std.stdio;
> import std.variant;
>
> void main()
> {
> 	Variant b = 56.051151;
> 	float x = b.coerce!float;
> 	writeln(x);
> }
>
>>56.0512

The precision is still there, you're just not requesting it:

import std.conv : text;
import std.stdio;
import std.variant;

void main()
{
     Variant b = 56.051151;
     float x = b.coerce!float;

     foreach (i; 0 .. 10)
         writefln(text("%.", i, "f"), x);
}

56
56.1
56.05
56.051
56.0512
56.05115
56.051151
56.0511513
56.05115128
56.051151276

> How to return exactly 56.051151 ?

Specify the number of digits after the decimal point manually, 
e.g. writefln("%.6f", x) will print 56.051151.


More information about the Digitalmars-d-learn mailing list