C#double转化成字符串保留小数位数

double temp=3.1415926;

1、固定小数位:(F)Fixed point

string str1=temp.toString("f1");//保留一位小数,四舍五入 结果:3.1
string str2=temp.toString("f2");//保留两位小数,四舍五入 结果:3.14

2、数字:(N)Number

string str2=temp.toString("N");//保留 结果:3.14

3、默认:(G)General (default)

string str2=temp.toString("G");//保留 结果:3.1415926

4、百分比:(P)Percent

string str2=temp.toString("P");//保留 结果:314.16%

5、科学计数法:(E)Scientific

string str2=temp.toString("E");//保留 结果E:3.141593E+000

6、货币金额:(C)Currency

string str2=temp.toString("C");//保留 结果:¥3.14
—— 完 ——