格式化字符
格式化字符 描述
f或F 格式化浮点数
e或E 用指数计数法格式化数字
p或P 格式化百分数
n或N 用逗号分隔符格式化数字
c或C 格式化本地货币值
d或D 格式化十进制数
g或G 用浮点数或指数计数法格式化数字
x或X 将浮点数转化为十六进制
{0,6}格式化变量中0指第一个变量,6表示显示宽度
/*
Example2_15.cs illustrates formatting numbers
*/
class Example2_15
{
public static void Main()
{
// formatting integers
int myInt = 12345;
int myInt2 = 67890;
System.Console.WriteLine("myInt = {0, 6}, myInt2 = {1, 5}",
myInt, myInt2);
System.Console.WriteLine("myInt using 10:d = {0, 10:d}",
myInt);
System.Console.WriteLine("myInt using 10:x = {0, 10:x2}",
myInt);
// formatting floating-point numbers
double myDouble = 1234.56789;
System.Console.WriteLine("myDouble using 10:f3 = {0, 10:f3}",
myDouble);
float myFloat = 1234.56789f;
System.Console.WriteLine("myFloat using 10:f3 = {0, 10:f3}",
myFloat);
decimal myDecimal = 1234.56789m;
System.Console.WriteLine("myDecimal using 10:f3 = {0, 10:f3}",
myDecimal);
System.Console.WriteLine("myFloat using 10:e3 = {0, 10:e3}",
myFloat);
System.Console.WriteLine("myFloat using 10:p2 = {0, 10:p2}",
myFloat);
System.Console.WriteLine("myFloat using 10:n2 = {0, 10:n2}",
myFloat);
System.Console.WriteLine("myFloat using 10:g2 = {0, 10:g2}",
myFloat);
// formatting currency values
decimal myMoney = 15123.45m;
System.Console.WriteLine("myMoney using 10:c2 = {0, 10:c2}",
myMoney);
string i = System.Console.ReadLine();
}
}