C 中變數 ToString 中引數介紹

2021-06-25 18:13:10 字數 3411 閱讀 6895

1. 取中文日期顯示_年月 currenttime.tostring("y"); 格式:2023年1月

2. 取中文日期顯示_月日 currenttime.tostring("m"); 格式:1月30日

3. 取日期顯示_年月日 currenttime.tostring("d"); 格式:2007-1-30

4. 取日期顯示_時分 currenttime.tostring("t"); 格式:15:35

5. int32.parse(變數) int32.parse("常量") 字元型轉換 轉為32位數字型

6. 變數.tostring("yyyymmdd") ; 格式:20070101c

貨幣2.5.tostring("c")

¥2.50

d十進位制數

25.tostring("d5")

00025

e科學型

25000.tostring("e")

2.500000e+005

f固定點

25.tostring("f2")

25.00g常規

2.5.tostring("g")

2.5n

數字2500000.tostring("n")

2,500,000.00

x十六進製制

255.tostring("x")

ff必須用「」將格式與其他字元分開。如果恰好在格式中也要使用大括號,可以用連續的兩個大括號表示乙個大括號,即: 「}」。

常用格式舉例:

(1) int i=12345;

this.textbox1.text=i.tostring();

//結果 12345(this指當前物件,或叫當前類的例項)

this.textbox2.text=i.tostring("d8");

//結果 00012345

(2) int i=123;

double j=123.45;

string s1=string.format("the value is ",i);

string s2=string.format("the value is ",j);

this.textbox1.text=s1 ;

//結果 the value is 123

this.textbox2.text=s2;

//結果 the value is 123.450

(3)double i=12345.6789;

this.textbox1.text=i.tostring("f2"); //結果 12345.68

this.textbox2.text=i.tostring("f6");

//結果 12345.678900

(4)double i=12345.6789;

this.textbox1.text=i.tostring("n"); //結果 12,345.68

this.textbox2.text=i.tostring(「n4」); //結果 12,345.6789

(5)double i=0.126;

string s=string.format("the value is ",i);

this.textbox1.text=i.tostring("p"); //結果 12.6%

this.textbox2.text=s; //結果 the value is 12.6%

(6) datetime dt =new datetime(2003,5,25);

this.textbox1.text=dt.tostring("yy.m.d");

//結果 03.5.25

this.textbox2.text=dt.tostring(「yyyy年m月」);

//結果 2023年5月

(7) int i=123;

double j=123.45;

string s=string.format("i:,j:",i,j);

//-7表示左對齊,佔7位

this.textbox1.text=s ;

//結果i:123 ,j: 123.45

datetime.now.tostring 中的引數問題

datetime.now.tostring根據引數不同輸出不同內容,功能很強的,總結一下。

[c#]

using system;

using system.globalization;

public class mainclass ;

string date;

for (int i = 0; i < format.length; i++)

/** output.

** d :08/17/2000

* d :thursday, august 17, 2000

* f :thursday, august 17, 2000 16:32

* f :thursday, august 17, 2000 16:32:32

* g :08/17/2000 16:32

* g :08/17/2000 16:32:32

* m :august 17

* r :thu, 17 aug 2000 23:32:32 gmt

* s :2000-08-17t16:32:32

* t :16:32

* t :16:32:32

* u :2000-08-17 23:32:32z

* u :thursday, august 17, 2000 23:32:32

* y :august, 2000

* dddd, mmmm dd yyyy :thursday, august 17 2000

* ddd, mmm d "'"yy :thu, aug 17 '00

* dddd, mmmm dd :thursday, august 17

* m/yy :8/00

* dd-mm-yy :17-08-00*/}

}格式字元 關聯屬性/說明

d shortdatepattern

d longdatepattern

f 完整日期和時間(長日期和短時間)

f fulldatetimepattern(長日期和長時間)

g 常規(短日期和短時間)

g 常規(短日期和長時間)

m、m monthdaypattern

r、r rfc1123pattern

s 使用當地時間的 sortabledatetimepattern(基於 iso 8601)

t shorttimepattern

t longtimepattern

u universalsortabledatetimepattern 用於顯示通用時間的格式

u 使用通用時間的完整日期和時間(長日期和長時間)

y、y yearmonthpattern

C 中ToString格式大全

字元型轉換為字串 c 貨幣 2.5.tostring c 2.50 d 10進製數 25.tostring d5 25000 e 科學型 25000.tostring e 2.500000e 005 f 固定點 25.tostring f2 25.00 f?表示保持幾位小數 g 常規 2.5.tos...

C 中ToString格式大全

c 貨幣2.5.tostring c 2.50 d十進位制數 25.tostring d5 00025 e科學型 25000.tostring e 2.500000e 005 f固定點 25.tostring f2 25.00g常規 2.5.tostring g 2.5n 數字2500000.tos...

C 中ToString格式大全

c 貨幣2.5.tostring c 2.50 d十進位制數 25.tostring d5 e科學型 25000.tostring e 2.500000e 005 f固定點 25.tostring f2 25.00g常規 2.5.tostring g 2.5n 數字2500000.tostring ...