1 const double number = 1.618033988749895; 2 double result; 3 string text; 4 5 text = string.Format("{0}", number); 6 result = double.Parse(text); 7 8 System.Console.WriteLine("{0}: result != number",result != number); 9 result = double.Parse(text);10 11 12 text = string.Format("{0:R}", number);13 result = double.Parse(text);14 System.Console.WriteLine("{0}: result == number", result == number);15 16 System.Console.ReadLine();
默认情况下,执行System.Console.WriteLine(1.618033988749895);语句会显示1.61803398874989,最后一个数位会被丢弃。为了更准确地支持与double值对应的字符串形式,我们可以使用格式字符串和round - trip 格式说明符R(或者r)进行转换。例如,string.Format("{0:R}", 1.61803398874989);会返回结果1.61803398874989。