C#中的格式化字符串文字
要使用C#格式化字符串文字,请使用String.Format方法。
在以下示例中,0是对象的索引,该对象的字符串值在该特定位置插入-
using System;
namespace Demo {
class Test {
static void Main(string[] args) {
decimal A = 15.2 m;
string res = String.Format("Temperature = {0}°C.", A);
Console.WriteLine(res);
}
}
}在下面的示例中,让我们为双精度类型设置字符串格式。
示例
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
热门推荐
10 亲戚生日红包祝福语简短
11 朋友花篮开业祝福语简短
12 祖国七十华诞简短祝福语
13 上嫂子生日祝福语简短
14 宝宝生日祝福语大全简短
15 新年给妹妹祝福语简短
16 对女友的简短祝福语
17 药店祝福语简短10字
18 团队文案祝福语简短励志