decimal a11 = 100100.01m; decimal a12 = 100100.51m; decimal a13 = 100100.50m; decimal a14 = 100100.00m; decimal a15 = 100100.55m; decimal a16 = 100100.54m; Console.WriteLine("#.##输出"); Console.WriteLine(a11.ToString("#.##")); Console.WriteLine(a12.ToString("#.##")); Console.WriteLine(a13.ToString("#.##")); Console.WriteLine(a14.ToString("#.##")); Console.WriteLine(a15.ToString("#.##")); Console.WriteLine(a16.ToString("#.##")); Console.WriteLine("#.#输出"); Console.WriteLine(a11.ToString("#.#")); Console.WriteLine(a12.ToString("#.#")); Console.WriteLine(a13.ToString("#.#")); Console.WriteLine(a14.ToString("#.#")); Console.WriteLine(a15.ToString("#.#")); Console.WriteLine(a16.ToString("#.#")); Console.WriteLine("0.00输出"); Console.WriteLine(a11.ToString("0.00")); Console.WriteLine(a12.ToString("0.00")); Console.WriteLine(a13.ToString("0.00")); Console.WriteLine(a14.ToString("0.00")); Console.WriteLine(a15.ToString("0.00")); Console.WriteLine(a16.ToString("0.00")); Console.WriteLine("0.0输出"); Console.WriteLine(a12.ToString("0.0")); Console.WriteLine(a11.ToString("0.0")); Console.WriteLine(a13.ToString("0.0")); Console.WriteLine(a14.ToString("0.0")); Console.WriteLine(a15.ToString("0.0")); Console.WriteLine(a16.ToString("0.0")); Console.WriteLine("F输出"); Console.WriteLine(a11.ToString("F")); Console.WriteLine(a14.ToString("F")); Console.WriteLine(a12.ToString("F")); Console.WriteLine(a13.ToString("F")); Console.WriteLine(a15.ToString("F")); Console.WriteLine(a16.ToString("F")); Console.WriteLine("####输出"); Console.WriteLine(a11.ToString("####")); Console.WriteLine(a12.ToString("####")); Console.WriteLine(a13.ToString("####")); Console.WriteLine(a14.ToString("####")); Console.WriteLine(a15.ToString("####")); Console.WriteLine(a16.ToString("####")); Console.WriteLine("F1"); Console.WriteLine(a11.ToString("F1")); Console.WriteLine(a12.ToString("F1")); Console.WriteLine(a13.ToString("F1")); Console.WriteLine(a14.ToString("F1")); Console.WriteLine(a15.ToString("F1")); Console.WriteLine(a16.ToString("F1")); Console.WriteLine("N"); Console.WriteLine(a11.ToString("N")); Console.WriteLine(a12.ToString("N")); Console.WriteLine(a13.ToString("N")); Console.WriteLine(a14.ToString("N")); Console.WriteLine(a15.ToString("N")); Console.WriteLine(a16.ToString("N")); Console.WriteLine("##.00"); Console.WriteLine(a11.ToString("##.00")); Console.WriteLine(a12.ToString("##.00")); Console.WriteLine(a13.ToString("##.00")); Console.WriteLine(a14.ToString("##.00")); Console.WriteLine(a15.ToString("##.00")); Console.WriteLine(a16.ToString("##.00")); Console.WriteLine("C"); Console.WriteLine(a11.ToString("C")); Console.WriteLine(a12.ToString("C")); Console.WriteLine(a13.ToString("C")); Console.WriteLine(a14.ToString("C")); Console.WriteLine(a15.ToString("C")); Console.WriteLine(a16.ToString("C"));
输出结果
#.##输出
100100.01
100100.51
100100.5
100100
100100.55
100100.54
#.#输出
100100
100100.5
100100.5
100100
100100.6
100100.5
0.00输出
100100.01
100100.51
100100.50
100100.00
100100.55
100100.54
0.0输出
100100.5
100100.0
100100.5
100100.0
100100.6
100100.5
F输出
100100.01
100100.00
100100.51
100100.50
100100.55
100100.54
####输出
100100
100101
100101
100100
100101
100101
F1
100100.0
100100.5
100100.5
100100.0
100100.6
100100.5
N
100,100.01
100,100.51
100,100.50
100,100.00
100,100.55
100,100.54
##.00
100100.01
100100.51
100100.50
100100.00
100100.55
100100.54
C
¥100,100.01
¥100,100.51
¥100,100.50
¥100,100.00
¥100,100.55
¥100,100.54
总结:
ToString("#.##"); //保留两位小数,会四舍五入,结尾为0则会省略输出
ToString("#.#"); //保留一位小数,会四舍五入,结尾为0则会省略输出
ToString("0.00"); //保留两位小数,会四舍五入,使用保留两位小数,没有两位小数会填充0补位
ToString("0.0"); //保留一位小数,会四舍五入,使用保留两位小数,没有小数会填充0补位
ToString("F"); //保留两位小数,会四舍五入,使用保留两位小数,没有两位小数会填充0补位
ToString("####"); //输出整数部分,会四舍五入
ToString("F1"); // 保留一位小数,会四舍五入,使用保留两位小数,没有小数会填充0补位
ToString("N"); // 保留两位小数,会四舍五入,使用保留两位小数,没有两位小数会填充0补位 ,整数部分每三位数一个[,]分隔符 例: 100,100.02
ToString("##.00"); //保留两位小数,会四舍五入,使用保留两位小数,没有两位小数会填充0补位
ToString("C"); // 输出部分会有人民币符号[¥],保留两位小数,会四舍五入,使用保留两位小数,没有两位小数会填充0补位,整数部分每三位数一个[,]分隔符 例:¥100,100.50
转发请携带本地址
标签:a15,格式化,a13,c#,ToString,WriteLine,Console,小数 From: https://www.cnblogs.com/tabai/p/18029955