C#中的格式化输出
要使用C#格式化输出,让我们看看格式化日期和双精度类型的示例。
将格式化的输出设置为Double类型。
示例
using System;
class Demo {
public static void Main(String[] args) {
Console.WriteLine("Three decimal places...");
Console.WriteLine(String.Format("{0:0.000}", 987.383));
Console.WriteLine(String.Format("{0:0.000}", 987.38));
Console.WriteLine(String.Format("{0:0.000}", 987.7899));
Console.WriteLine("Thousands Separator...");
Console.WriteLine(String.Format("{0:0,0.0}", 54567.46));
Console.WriteLine(String.Format("{0:0,0}", 54567.46));
}
}输出结果
Three decimal places... 987.383 987.380 987.790 Thousands Separator... 54,567.5 54,567
设置DateTime的格式化输出
示例
using System;
static class Demo {
static void Main() {
DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123);
Console.WriteLine(String.Format("{0:y yy yyy yyyy}", d));
Console.WriteLine(String.Format("{0:M MM MMM MMMM}", d));
Console.WriteLine(String.Format("{0:d dd ddd dddd}", d));
}
}输出结果
18 18 2018 2018 2 02 Feb February 8 08 Thu Thursday
热门推荐
1 孙媳妇祝福语简短
10 麻辣拌店祝福语简短
11 餐前仪式祝福语简短
12 新婚红包祝福语简短男
13 新年探亲祝福语简短精辟
14 培训送别祝福语简短的话
15 关于扬州的祝福语简短
16 圣诞简短祝福语给父母
17 毕业祝福语保安文案简短
18 新婚抖音祝福语简短