string to real conversion losing data

bearophile bearophileHUGS at lycos.com
Thu Apr 8 14:36:20 PDT 2010


> If I add these numbers, the outcome should be 17744.06.  Any ideas?  I am using Digital Mars D Compiler v1.046.
> josé

This prints the same values, using latest D2:


import std.stdio, std.conv;

void main() {
    real tot = 0;
    foreach(el; ["14539.34","1230.00","361.62","1613.10","0.00"])
        tot += to!real(el);
    writefln("%.5f\n", tot); // 17744.06000

    tot = 0;
    foreach(el; [14539.34,1230.00,361.62,1613.10,0.00])
        tot += to!real(el);
    writefln("%.5f\n", tot); // 17744.06000
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list