string to real conversion losing data

jicman cabrera_ at _wrc.xerox.com
Fri Apr 9 22:19:58 PDT 2010


Ali Çehreli Wrote:

> jicman wrote:
> > Greetings and salutations!
> > 
> > Will someone be so kind as to explain why this is happening?
> > 
> > ----
> > import std.stdio;
> > import std.conv;
> > 
> > void main()
> > {
> >   char[][] strRealVals =
> >   [
> >     "14539.34","1230.00","361.62","1613.10","","","0.00"
> >   ];
> >   real rTotal = 0;
> >   foreach (char[] s; strRealVals)
> >   {
> >     writefln("Real value is: " ~ s);
> >     real r = 0.00;
> >     if (s != "")
> >       r = std.conv.toReal(s);
> >     rTotal += r;
> >   }
> >   writefln(std.string.toString(rTotal));
> >   writefln(rTotal);
> > }
> > ----
> > 
> > When I run this program, I get this:
> > 16:51:35.54>realtest
> > Real value is: 14539.34
> > Real value is: 1230.00
> > Real value is: 361.62
> > Real value is: 1613.10
> > Real value is:
> > Real value is:
> > Real value is: 0.00
> > 17744.1
> > 17744.1
> > 
> > ----
> > 
> > If I add these numbers, the outcome should be 17744.06.  Any ideas?  I am using Digital Mars D Compiler v1.046.
> > 
> > thanks,
> > 
> > jos�
> > 
> 
> The default format string for floating point values is 6 decimal digits 
> of precision (the digits before and after the decimal point). writefln 
> is rounding the value up.
> 
> Try for example "%.18f" for 'real'. This does it automatically:
> 
> import std.stdio;
> import std.string;
> 
> void main()
> {
>      auto theValue = 17744.06L;
> 
>      writefln("%s has %s decimal digits of precision",
>               typeof(theValue).stringof, theValue.dig);
> 
>      auto formatString = format("%%.%sf", theValue.dig);
>      writefln(formatString, theValue);
> }
> 

thanks.  That worked.

jic


More information about the Digitalmars-d-learn mailing list