Hello,
I found a strange behavior in the ToString() function.
Reproducible code:
// initialization
string strText;
double dblTemperature;
dblTemperature = 0.09999;
strText = dblTemperature.ToString("F3");
Debug.WriteLine("Value 1: " + strText);
dblTemperature = -0.09999;
strText = dblTemperature.ToString("F3");
Debug.WriteLine("Value 2: " + strText);
dblTemperature = 0.099999999999999978;
strText = dblTemperature.ToString("F3");
Debug.WriteLine("Value 3: " + strText);
(dblTemperature can assume any value between -5 and 65)
It works always except when dblTemperature contains the value: 0.099999999999999978 where the .ToString(âF3â) statement throws an exception.
The code output is the following:
Value 1: 0.100
Value 2: -0.100
#### Exception System.Exception - CLR_E_WRONG_TYPE (1) ####
#### Message:
#### System.String::get_Chars [IP: 0000] ####
#### System.Number::ReplaceNegativeSign [IP: 0005] ####
#### System.Number::PostProcessFloat [IP: 0018] ####
#### System.Number::Format [IP: 0143] ####
#### System.Double::ToString [IP: 002e] ####
Generated Exception: âSystem.Exceptionâ in mscorlib.dll
Uncought Exception of type âSystem.Exceptionâ in mscorlib.dll
I donât know if there are other cases. What can be the reason?
Iâm using TinyCLR V2.1.0 on SCM20260.
Thanks in advance for any suggestion.