C#数值格式

示例

// 整数类型为十六进制
string.Format("Hexadecimal: byte2: {0:x2}; byte4: {0:X4}; char: {1:x2}", 123, (int)'A');

// 带有千位分隔符的整数
string.Format("Integer, thousand sep.: {0:#,#}; fixed length: >{0,10:#,#}<", 1234567);

// 带有前导零的整数
string.Format("Integer, leading zeroes: {0:00}; ", 1);

// 小数点
string.Format("Decimal, fixed precision: {0:0.000}; as percents: {0:0.00%}", 0.12);

输出:

Hexadecimal: byte2: 7b; byte4: 007B; char: 41
Integer, thousand sep.: 1,234,567; fixed length: > 1,234,567<
Integer, leading zeroes: 01; 
Decimal, fixed precision: 0.120; as percents: 12.00%