有時,我們可能需要將數值以一定的格式來呈現,就需要對數值進行格式化。
格式說明符
說明示例輸出c
貨幣15.tostring(「c」)
¥15.00
15.tostring(「c1」)
¥15.0
15.tostring(「c3」)
¥15.000
d十進位制數
15.tostring(「d1」)
1515.tostring(「d2」)
1515.tostring(「d5」)
00015
e科學型
15.tostring(「e」)
1.500000e+001
1500.tostring(「e」)
1.500000e+003
0.15.tostring(「e」)
1.500000e-001
0.0015.tostring(「e」)
1.500000e-003
0.0015.tostring(「e0」)
2e-003
0.0015.tostring(「e1」)
1.5e-003
0.0015.tostring(「e2」)
1.50e-003
f固定點
15.tostring(「f1」)
15.0
15.tostring(「f2」)
15.00g常規
2.5.tostring(「g」)
2.5n
數字1500000.tostring(「n」)
1,500,000.00
1500000.tostring(「n0」)
1,500,000
1500000.tostring(「n1」)
1,500,000.0
x十六進製制
15.tostring(「x」)
f15.tostring(「x3」)
00f字元
說明示例輸出c
貨幣string.format("", 2)
$2.000
d十進位制
string.format("", 2)
002e
科學計數法
1.20e+001
1.20e+001g常規
string.format("", 2)2n
用分號隔開的數字
string.format("", 250000)
250,000.00
x十六進製制
string.format("", 12)c0
零佔位符
string.format("", 12.3)
012.300
#數字佔位符
string.format("", 12.3)
12string.format("", 12.3)
12string.format("", 12.3)
12string.format("", 12.3)
12.3
「0」描述:佔位符,如果可能,填充位
string.format("", 1234); // 結果:001234
「#」描述:佔位符,如果可能,填充位
string.format("", 1234); // 結果:1234
string.format("", 1234); // 結果:01234
string.format("", 1234); // 結果:0001234
"."描述:小數點
string.format("", 1234); // 結果:1234.000
string.format("", 4321.12543); // 結果:4321.125
","描述:數字分組,也用於增倍器
string.format("", 1234567); // 結果:1,234,567
"%"描述:格式為百分數
string.format("", 1234); // 結果:123400%
string.format("", 1234.125); // 結果:123413%
string.format("", 1234); // 結果: 123400.00%
string.format("", 1234.125); // 結果:123412.50%
"abc"描述:顯示單引號內的文字
string.format("", 1234); // 結果:文字1234
""描述:用於轉移符
string.format("\"abc!\""); // 結果:abc!
"@"描述:後跟要列印字串
string.format(@""abc!""); // 結果:abc!
ToString 格式化數值
格式化數值 有時,我們可能需要將數值以一定的格式來呈現,就需要對數值進行格式化。我們使用格式字串指定格式。格式字串採用以下形式 axx,其中 a 為格式說明符,指定格式化型別,xx 為精度說明符,控制格式化輸出的有效位數或小數字數。格式說明符 說明示例輸出c 貨幣2.5.tostring c2 2....
C 之 數字格式化
格式規範的完整形式 index是此格式程式引用的格式字串之後的引數,從零開始計數 width 可選 是要設定格式的字段的寬度,width取正數表示結果右對齊,取負數表示結果左對齊 formatstring 可選 包含有關設定型別格式的格式說明。一.內建型別的字母格式 1.e 科學計數法表示 2500...
C 之 GUID格式化
guid的帶引數的tostring 方法來實現格式化,如下 摘要 根據所提供的格式說明符,返回此 system.guid 例項值的字串表示形式。引數 format 乙個單格式說明符,它指示如何格式化此 system.guid 的值。format 引數可以是 n d b p 或 x 如果 format...