Main Site Documentation

Convert.ToDouble() stray errors in result


#1

Just an observation that may affect some peoples calculations.

NETMF 4.1.6 Fez Cobra

[Code]
String sVal =“4.789876”;

double dVal = Convert.ToDouble(sVal);

// dVal = 4.789876

String sResult = dVal.ToString(dVal)

//sResult = “4.7898759999999996”
[/Code]

This doesn’t happen in .NET Windows Applications development only in .NET Micro Framework.

Is this a bug, or just something to live with?


#2

Exception: in 4.1.6


                double dVal = 0.0;
                String sVal = "1234568.800098";      //simulate user input
                try   //added incase user input not numeric
                {
                    dVal = Convert.ToDouble(sVal);
                    //dVal = 1234568.8000979999
                }
                catch (Exception)
                {
                    dVal = 0.0;
                }

                //d.dd format (String.Format) not supported in NETMF
                mResponseText = dVal.ToString("0.00");
                //Get Exception in line above
                //Ex='System.ArgumentException' occurred in mscorlib.dll


only if I modify the above to remove the format string, does it stop crashing


              mResponseText = dVal.ToString();
  

But this introduces a value error in the string conversion.
such that the result is “1234568.8000979999” when I only want the display to
show “1234568.80”


#3

Solved:
http://www.tinyclr.com/forum/2/3716/

Where can I find a list of all the NETMF ToString(?) formats?

So far by using a trawl net I’ve only found
ToString( “X” ) for Hex
ToString( “X8” ) for Hex
ToString( “F2” ) for doubles d.dd