{<index>} // eg:{0} {1} {2}
例子
class Program
{
static void Main(string[] args)
{
string nameCat = "Tom";
string nameMouse = "Jerry";
Console.WriteLine("Hello {0} and {1}",nameCat,nameMouse );
}
}
// Hello Tom and Jerry
//
以例子来看
class Program
{
static void Main(string[] args)
{
double _Area = 676.454;
Console.WriteLine("Area is {0,-9:F2}", _Area);
}
}
// Area is 676.45
//
格式化输出的占位符为{0,-9:F2}
,以:
将大括号分为两部分,
:
左边是输出内容位置的格式化设置,
其中0
表示是第0号占位符(索引值从零开始),9
表示该项输出占9位,-
表示左对齐(无-
则是右对齐);
:
右边是输出内容数值类型的格式化设置,
其中F
表示以浮点类型输出,,2
表示小数点后保留两位。
原文:https://www.cnblogs.com/moyutime/p/14620023.html