C++中Format的用法
阿新 • • 發佈:2018-12-30
Format函式使用說明
一. 字串
函式的宣告:
function Format(const Format: string; const Args: array of const): string; overload;
Format引數是一個格式字串,用於格式化Args裡面的值的。Args是一個變體陣列,即它裡面可以有多個引數,而且每個引數可以不同。
Args的格式為:”%” [index “:”] [“-“] [width] [“.” prec] type
格式名稱 | explain | example |
---|---|---|
[index “:”] | Args第幾個引數 | Format(“this is %1:d %0:d”,12,13);結果:this is 13 12 |
“-“ | 引數向左齊,和[width]合在一起 | Format(“this is %-4d,”,12);結果:this is 12 , |
width | 格式化得值佔得寬度 | |
“.”prec | 指定精度 | Format(‘this is %.7f’,1.1234]);結果:this is 1.1234000 |
type | 如下表的含義 |
Type | explain | example |
---|---|---|
d | 十進位制的數,int型別 | |
u | 無符號的十進位制數,2^32-負數的絕對值 | Format(“%u”,-2);結果:4294967294 |
f | 浮點數 | |
e | 科學表示法 | Format(“%e”,-2.22);結果:-2.2200000E+000 |
g | 將浮點值中的多餘數去掉 | Format(“%g”,02.000);結果:2.2 |
n | 將浮點型轉化為號碼形式 | Format(“%n”,4552.2176),結果:4,522.22 |
P | 將指標型別的值返回 | Format(“%P”,P),結果:0012F548 |
m | 將浮點數格式化錢幣型 | Format(“%m”,9552.21),結果:¥9,552.21 |
s | 字串 | |
X | 將整形以十六進位制返回 | Format(“%X”,15),結果:F |
x | 將整形以十六進位制返回 | Format(“%x”,15),結果:f |