C 數字格式字串

2022-02-10 03:18:17 字數 4640 閱讀 4649

使用c#格式化字串

1 前言

如果你熟悉microsoft foundation

classes(mfc)的cstring,windows template library(wtl)的cstring或者standard

template

library(stl)的字串類,那麼你對string.format方法肯定很熟悉。在c#中也經常使用這個方法來格式化字串,比如下面這樣:

int x = 16;

decimal y = 3.57m;

string h = string.format( "item sells at ", x, y );

console.writeline(h);

在我的機器上,可以得到下面的輸出:

item 16 sells at ¥3.57

也許你的機器上的輸出和這個不太一樣。這是正常的,本文稍後就會解釋這個問題。

在我們日常使用中,更多的是使用console.writeline方法來輸出乙個字串。其實string.format和

console.writeline有很多共同點。兩個方法都有很多過載的格式並且採用無固定引數的物件陣列作為最後乙個引數。下面的兩個語句會產生同樣

整)格式僅僅對浮點數有效。這個值首先會用通用格式來格式化。對於雙精度數有15位精度,對於單精度數有7位精度。如果這個值可以被正確地解析回原始的數

字,就會用通用格式符來格式化。如果不能解析回去的話,那麼就會用17位精度來格式化雙精度數,用9位精度來格式化單精度數。儘管我們可以在圓整識別符號後

意到,如果你要使用numberstyles,就要新增對system.globalization的引用,然後就可以使用不同numberstyles

的組合或者其中的任意一種。如果你想相容貨幣符號,就需要使用過載的parse方法,它們採用了numberformatinfo物件作為乙個引數,然後

你可以設定numberformatinfo的currencysymbol屬性來呼叫parse方法,比如:

string u = "¥ -1,234,567.890 ";

numberformatinfo ni = new numberformatinfo();

ni.currencysymbol = "¥";

double h = double.parse(u, numberstyles.any, ni);

console.writeline("h = ", h);

上面的**有如下輸出

h = -1234567.89

除了numberformatinfo,還可以使用cultureinfo類。cultureinfo代表了某種特定的文化,包括文化的名字,書寫的方式,

日曆的格式。對於某種特定文化的操作是非常普遍的情況,比如格式化日期和排序。文化的命名方式遵從rfc1766標準,使用《語言**

2>-《國家/地區碼2>的方式,其中的《語言**2>是兩個小寫的字母,它們來自iso639-1;《國家/地區

碼2>是兩個大寫字母,它們來自iso3166。比如,美國英語是「en-us"。英國英語是"en-gb"。千里達及托巴哥英語是"en-

tt"。例如,我們可以建立乙個美國英語的cultureinfo物件並且基於這種文化將數字轉換成字串。

int k = 12345;

cultureinfo us = new cultureinfo("en-us");

string v = k.tostring("c", us);

console.writeline(v);

輸出是:

$12,345.00

要注意到,我們使用了過載的tostring方法,它把第乙個格式化字串當成第乙個引數,將乙個cultureinfo物件(執行了iformatprovider物件)作為第二個引數。這兒有第二個例子,對於丹麥人來說:

cultureinfo dk = new cultureinfo("da-dk");

string w = k.tostring("c", dk);

console.writeline(w);

輸出是:

kr 12.345,00

5 字串和日期

乙個日期物件有個叫ticks的屬性。它儲存了自從公元1年的1月1號上午12點開始的,以100納秒為間隔的時間。比如,ticks值等於

屬性得到了預設的唯讀的datetimeformatinfo例項,它與文化無關。你可以建立自定義的模式。要注意到的是invariantinfo不一

定和本地的格式一樣。invariant等於美國格式。另外,如果你向datetime.format方法傳遞的第二個引數是

null,datetimeformatinfo將會是預設的currentinfo。比如

console.writeline(dt.tostring("d", dtfi));

console.writeline(dt.tostring("d", null));

console.writeline();

輸出是

06/23/2001

23/06/2001

對比選擇invariantinfo和currentinfo的。

datetimeformatinfo dtfi;

console.write("[i]nvariant or [c]urrent info?: ");

if (console.read() == 'i')

dtfi = datetimeformatinfo.invariantinfo;

else

dtfi = datetimeformatinfo.currentinfo;

datetimeformatinfo dtfi = datetimeformatinfo.invariantinfo;

console.writeline(dt.tostring("d", dtfi));

console.writeline(dt.tostring("f", dtfi));

console.writeline(dt.tostring("f", dtfi));

console.writeline(dt.tostring("g", dtfi));

console.writeline(dt.tostring("g", dtfi));

console.writeline(dt.tostring("m", dtfi));

console.writeline(dt.tostring("r", dtfi));

console.writeline(dt.tostring("s", dtfi));

console.writeline(dt.tostring("t", dtfi));

console.writeline(dt.tostring("t", dtfi));

console.writeline(dt.tostring("u", dtfi));

console.writeline(dt.tostring("u", dtfi));

console.writeline(dt.tostring("d", dtfi));

console.writeline(dt.tostring("y", dtfi));

console.writeline(dt.tostring("dd-mmm-yy", dtfi));

輸出是

[i]nvariant or [c]urrent info?: i

01/03/2002

03/01/2002

thursday, 03 january 2002

thursday, 03 january 2002 12:55

thursday, 03 january 2002 12:55:03

01/03/2002 12:55

01/03/2002 12:55:03

january 03

thu, 03 jan 2002 12:55:03 gmt

2002-01-03t12:55:03

12:55

12:55:03

2002-01-03 12:55:03z

thursday, 03 january 2002 12:55:03

01/03/2002

2002 january

03-jan-02

[i]nvariant or [c]urrent info?: c

03/01/2002

03/01/2002

03 january 2002

03 january 2002 12:55

03 january 2002 12:55:47

03/01/2002 12:55

03/01/2002 12:55:47

03 january

thu, 03 jan 2002 12:55:47 gmt

2002-01-03t12:55:47

12:55

12:55:47

2002-01-03 12:55:47z

03 january 2002 12:55:47

03/01/2002

january 2002

03-jan-02

更過資訊,請參考:msdn

標準數字格式字串

格式說明符 名稱 說明 c 或 c 貨幣 數字轉換為表示貨幣金額的字串。轉換由用於格式化數字的 numberformatinfo 物件的貨幣格式資訊控制。精度說明符指示所需的小數字數。如果省略精度說明符,則使用 numberformatinfo 給定的預設貨幣精度。d 或 d 十進位制 只有整型才支...

c 字串轉數字或數字轉字串

在c 中字串轉換為數字,或數字轉換為字串,用到如下函式 itoa atoi atof itoa itow itoa s 1.整形轉換為字串 2.字串轉為整形 在字符集設定不同下會有不同的型別,說白了,這幾個函式的功能都相同,但是根據你的字符集不同,選用的函式也不同。itot 在asicii下被巨集定...

c 格式字串說明

c 的格式化字串經常用作格式化數字的輸出 字串合併和轉換等等很多場合。1.格式化規定符 符號 作用 d 十進位制有符號整數 u 十進位制無符號整數 f 浮點數 s 字串 c 單個字元 p 指標的值 e 指數形式的浮點數 x,x 無符號以十六進製制表示的整數 0 無符號以八進位制表示的整數 g 自動選...