把doubel保留2位有效数字
double d = 123;
string res = string.Format("{0:F2}",d);
结果是:d = 123.00
参考:
string str1 =string.Format("{0:N1}",56789); //result: 56,789.0 string str2 =string.Format("{0:N2}",56789); //result: 56,789.00 string str3 =string.Format("{0:N3}",56789); //result: 56,789.000 string str8 =string.Format("{0:F1}",56789); //result: 56789.0 string str9 =string.Format("{0:F2}",56789); //result: 56789.00 string str11 =(56789 / 100.0).ToString("#.##"); //result: 567.89 string str12 =(56789 / 100).ToString("#.##"); //result: 567 C 或 c 货币 Console.Write("{0:C}", 2.5); //$2.50 Console.Write("{0:C}", -2.5); //($2.50) D 或 d 十进制数 Console.Write("{0:D5}", 25); //00025 E 或 e 科学型 Console.Write("{0:E}", 250000); //2.500000E+005 F 或 f 固定点 Console.Write("{0:F2}", 25); //25.00 Console.Write("{0:F0}", 25); //25 G 或 g 常规 Console.Write("{0:G}", 2.5); //2.5 N 或 n 数字 Console.Write("{0:N}", 2500000); //2,500,000.00 X 或 x 十六进制 Console.Write("{0:X}", 250); //FA Console.Write("{0:X}", 0xffff); //FFFF