輸出方法Console. WriteLine( )
Console. WriteLine()方法將數據輸出到屏幕并加上一個回車換行符(若不加回車換行
符,可用Console. Write()方法)。
該方法類似于C語言中的printf()函數,
可以采用“{N[,M][:格式化字符串]}
”的形式格式化輸出字符串,其中的參數含義如下。
①花括號({}
): 用來在輸出字符串中插人變量的值。
②N
:表示輸出變量的序號,從0開始,例如當N為0時,對應輸出第1個變量的值;當
N為2時,對應輸出第3個變量的值,依此類推。
③[,M]
:可選項,其中M表示輸出的變量所占的字符個數。當這個值為負數時,輸出的變量按照左對齊方式排列;如果這個值為正數,輸出的變量按照右對齊方式排列。
④[:格式化字符串]
:可選項,因為在向控制臺輸出時常常需要指定輸出字符串的格式。
通過使用標準數字格式字符串,可以使用Xn的形式來指定結果字符串的格式,其中X
指定數字的格式,n指定數字的精度,即有效數字的位數。這里提供了8個常用的格
式字符。
注意:在一個Write/ WriteLine方法中,N的序號是連續的,且從0開始。例如,以下語句
都是錯誤的:
Console. WriteLine("{0} and {2}",1.2);//序號不連續
Console. WriteLine("{1} and {2}".1,2);//序號不是從0開始的
格式字符 | 含義 | 示例 | 輸出結果 |
---|---|---|---|
C或c | 將數據轉換成貨幣格式 | Console. WriteLine("{0,5:c}", 123. 456); | ¥123.46 |
D或d | 整數數據類型格式 | Console. WriteLine("{0:D4}", 123); | 0123 |
E或e | 科學記數法格式 | Console. W riteLine("{0:E4}", 123. 456); | 1.2346E十002 |
F或f | 浮點數據類型格式 | Console. WriteLine("{0:f4}", 123. 456); | 123.4560 |
G或g | 通用格式 | Console. WriteLine("{0:g)", 123. 456); | 123.456 |
N或n | 自然數據格式 | Console. WriteLine("{0:n}", 123. 456); | 123.46 |
X或x | 十六進制數據格式 | Console. WriteLine("{0:x}", 12345); | 3039 |
舉個詳細的例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace day2_25
{class Program{static void Main(string[] args){double d = 123.456;Console.WriteLine("d={0:c2}",d);//d=¥123.46Console.WriteLine("d={0:c3}", d);//d=¥123.456Console.WriteLine("d={0:c4}", d);//d=¥123.4560Console.WriteLine("d={0,5:c2}", d);//d=¥123.46Console.WriteLine("d={0,6:c3}", d);//d=¥123.456Console.WriteLine("d={0,7:c4}", d);//d=¥123.4560Console.WriteLine("d={0,8:c4}", d);//d=¥123.4560//由上面的例子可以得出:{N[,M][:格式化字符串]} [:格式化字符串]的優先級要比[,M]高//即d={0,8:c4},.8代表寬度為8位,而c4代表將數據轉換成貨幣格式且保留4位小數位,由于保留4位小數,故d只能為123.4560,7位數Console.WriteLine("d={0:e4}",d);//d=1.2346e+002Console.WriteLine("d={0,3:e5}", d);//d=1.23456e+002Console.WriteLine("d={0,4:e6}", d);//d=1.234560e+002//d={0,4:e6}其中e6表示6位小數位數Console.WriteLine("d={0:f4}",d);//d=123.4560Console.WriteLine("d={0:f5}", d);//d=123.45600Console.WriteLine("d={0:f6}", d);//d=123.456000Console.WriteLine("d={0,4:f4}", d);//d=123.4560Console.WriteLine("d={0,5:f5}", d);//d=123.45600Console.WriteLine("d={0,10:f10}", d);//d=123.4560000000//d={0,10:f10}其中f10表示浮點數的小數位數為10位,總寬度也為10位,如果相沖突,以后面的小數位數為準Console.WriteLine("d={0:g}",d);//d=123.456Console.WriteLine("d={0:g5}", d);//d=123.46Console.WriteLine("d={0:g6}", d);//d=123.456Console.WriteLine("d={0:g7}", d);//d=123.456//d={0:g7}按理說會有7位小數,但是由于d本身是123.456,而且運用的是g,所以精讀最高到它本身,再多也無用//d={0:g}默認為原樣輸出Console.WriteLine("d={0:n}", d);//d=123.46Console.WriteLine("d={0:n2}", d);//d=123.46Console.WriteLine("d={0:n3}", d);//d=123.456Console.WriteLine("d={0:n4}", d);//d=123.4560Console.WriteLine("d={0:n5}", d);//d=123.45600Console.WriteLine("d={0:n6}", d);//d=123.456000Console.WriteLine("d={0:n7}", d);//d=123.4560000//d={0:n}默認為n2,保留兩位小數位數Console.ReadLine();}}
}
運行輸出結果如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace yy
{class Program{static void Main(string[] args){int a = 1234, b = -1234;Console.WriteLine("a={0},b={1}", a, b);//a=1234,b=-1234Console.WriteLine("a={0:D5},b={1:D5}", a, b);//a=01234,b=-01234Console.WriteLine("a={0:c3},b={1:c2}", a, b);//a=¥1,234.000,b=¥-1,234.00//a={0:D5},b={1:D5}其中D5表示一共5位數//a={0:c3},b={1:c2}其中c3和c2表示小數位數分別為3位和2位Console.ReadLine();}}
}
運行輸出結果如下: