国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

String.Format大全(C# Java)

2019-10-29 21:38:54
字體:
來源:轉載
供稿:網友

String.format無論是在C#中還是在java中應用都非常廣泛,今天小編抽個時間把有關string.format知識總結分享給大家,需要的朋友可以參考下

c#string.format

字符串的數字格式

 

 
  1. stringstr1 =string.Format("{0:N1}",56789); //result: 56,789.0 
  2. stringstr2 =string.Format("{0:N2}",56789); //result: 56,789.00 
  3. stringstr3 =string.Format("{0:N3}",56789); //result: 56,789.000 
  4. stringstr8 =string.Format("{0:F1}",56789); //result: 56789.0 
  5. stringstr9 =string.Format("{0:F2}",56789); //result: 56789.00 
  6. stringstr11 =(56789 / 100.0).ToString("#.##"); //result: 567.89 
  7. stringstr12 =(56789 / 100).ToString("#.##"); //result: 567 

C 或 c

貨幣

 

 
  1. Console.Write("{0:C}", 2.5); / 2.50 
  2. Console.Write("{0:C}", -2.5); //($2.50) 

D 或 d

十進制數

 

 
  1. Console.Write("{0:D5}", 25); //00025 

E 或 e

科學型

 

 
  1. Console.Write("{0:E}", 250000); //2.500000E+005 

F 或 f

固定點

 

 
  1. Console.Write("{0:F2}", 25); //25.00 
  2. Console.Write("{0:F0}", 25); //25 

G 或 g

常規

 

 
  1. Console.Write("{0:G}", 2.5); //2.5 

N 或 n

數字

 

 
  1. Console.Write("{0:N}", 2500000); //2,500,000.00 

X 或 x

十六進制

 

 
  1. Console.Write("{0:X}", 250); //FA 
  2. Console.Write("{0:X}", 0xffff); //FFFF 

/////////////////////////////////////////////////////////////////////////////////

C#格式化數值結果表

String.Format大全(C# Java)

Strings

There really isn't any formatting within a strong, beyond it's alignment. Alignment works for any argument being printed in a String.Format call.

String.Format大全(C# Java)

Numbers

Basic number formatting specifiers:

String.Format大全(C# Java)

Custom number formatting:

String.Format大全(C# Java)

Dates

Note that date formatting is especially dependant on the system's regional settings; the example strings here are from my local locale.

String.Format大全(C# Java)

Custom date formatting:

String.Format大全(C# Java)

Enumerations

String.Format大全(C# Java)

Some Useful Examples

String.Format("{0:$#,##0.00;($#,##0.00);Zero}", value);

This will output "$1,240.00" if passed 1243.50. It will output the same format but in parentheses if the number is negative, and will output the string "Zero" if the number is zero.

String.Format("{0:(###) ###-####}", 18005551212);

This will output "(800) 555-1212".

變量.ToString()

字符型轉換 轉為字符串

12345.ToString("n"); //生成 12,345.00

12345.ToString("C"); //生成 ¥12,345.00

12345.ToString("e"); //生成 1.234500e+004

12345.ToString("f4"); //生成 12345.0000

12345.ToString("x"); //生成 3039 (16進制)

12345.ToString("p"); //生成 1,234,500.00%

java字符串格式化:String.format()方法的使用大全

字符型轉換 轉為字符串

12345.ToString("n"); //生成 12,345.00

12345.ToString("C"); //生成 ¥12,345.00

12345.ToString("e"); //生成 1.234500e+004

12345.ToString("f4"); //生成 12345.0000

12345.ToString("x"); //生成 3039 (16進制)

12345.ToString("p"); //生成 1,234,500.00%

java字符串格式化:String.format()方法的使用大全

字符串格式化,即按照你想要的字符串格式輸出,如時間顯示格式等,下面這個網站介紹的相當全面。

參考網站:http://kgd1120.iteye.com/blog/1293633:

鏈接點擊: String.format()方法的使用大全

實例:在android系統里面進行文件操作時,有時會使用當前時間,作為要保存數據的文件名,以便區別文件及日后對此文件進行操作。就如錄像文件,當日保存下來的錄像肯定會用當日的時間作為文件名稱的一部分,方便以后查看。

如:

 

 
  1. Time t = new Time(); 
  2. t.setToNow();  
  3. String filename = String.format("/record%04d%02d%02d%02d%02d%02d.avi"
  4. t.year, t.month+1 , t.monthDay, t.hour, t.minute, t.second); 

音視頻項目中經常會用此文件命名方式。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 尼玛县| 武安市| 台安县| 湘潭市| 乐亭县| 武川县| 杂多县| 五原县| 越西县| 盘锦市| 孝昌县| 会东县| 乌兰察布市| 上高县| 太仓市| 垣曲县| 来宾市| 高邮市| 屏山县| 广平县| 清水河县| 郴州市| 玛曲县| 长白| 伊春市| 通山县| 宁化县| 调兵山市| 平安县| 凤凰县| 凤台县| 新绛县| 于都县| 福海县| 广饶县| 汤原县| 南雄市| 昌都县| 绥江县| 临澧县| 信丰县|