格式化输出
格式化输出:
1 Console.WriteLine("平均值为:{0:f2}",Program.GetAverage(a)); 2 Console.WriteLine("平均值为:{0:0.00}",Program.GetAverage(a));
占位符会四舍五入
例:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication3 8 { 9 class Program 10 { 11 /* 12 * 占位符的四舍五入 13 */ 14 static void Main(string[] args) 15 { 16 double a = 1.248; 17 18 Console.WriteLine("{0:0.00}", a); 19 Console.ReadKey(); 20 } 21 } 22 }
输出结果:
1.25