String.Format大全(C# Java)
c#string.format
字符串的數(shù)字格式
stringstr1 =string.Format("{0:N1}",56789); //result: 56,789.0 stringstr2 =string.Format("{0:N2}",56789); //result: 56,789.00 stringstr3 =string.Format("{0:N3}",56789); //result: 56,789.000 stringstr8 =string.Format("{0:F1}",56789); //result: 56789.0 stringstr9 =string.Format("{0:F2}",56789); //result: 56789.00 stringstr11 =(56789 / 100.0).ToString("#.##"); //result: 567.89 stringstr12 =(56789 / 100).ToString("#.##"); //result: 567
C 或 c
貨幣
Console.Write("{0:C}", 2.5); / 2.50 Console.Write("{0:C}", -2.5); //($2.50)
D 或 d
十進(jìn)制數(shù)
Console.Write("{0:D5}", 25); //00025
E 或 e
科學(xué)型
Console.Write("{0:E}", 250000); //2.500000E+005
F 或 f
固定點(diǎn)
Console.Write("{0:F2}", 25); //25.00 Console.Write("{0:F0}", 25); //25
G 或 g
常規(guī)
Console.Write("{0:G}", 2.5); //2.5
N 或 n
數(shù)字
Console.Write("{0:N}", 2500000); //2,500,000.00
X 或 x
十六進(jìn)制
Console.Write("{0:X}", 250); //FA Console.Write("{0:X}", 0xffff); //FFFF
/////////////////////////////////////////////////////////////////////////////////
C#格式化數(shù)值結(jié)果表
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.
Numbers
Basic number formatting specifiers:
Custom number formatting:
Dates
Note that date formatting is especially dependant on the system's regional settings; the example strings here are from my local locale.
Custom date formatting:
Enumerations
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()
字符型轉(zhuǎn)換 轉(zhuǎn)為字符串
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進(jìn)制)
12345.ToString("p"); //生成 1,234,500.00%
java字符串格式化:String.format()方法的使用大全
字符型轉(zhuǎn)換 轉(zhuǎn)為字符串
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進(jìn)制)
12345.ToString("p"); //生成 1,234,500.00%
java字符串格式化:String.format()方法的使用大全
字符串格式化,即按照你想要的字符串格式輸出,如時(shí)間顯示格式等,下面這個(gè)網(wǎng)站介紹的相當(dāng)全面。
參考網(wǎng)站:http://kgd1120.iteye.com/blog/1293633:
鏈接點(diǎn)擊: String.format()方法的使用大全
實(shí)例:在android系統(tǒng)里面進(jìn)行文件操作時(shí),有時(shí)會(huì)使用當(dāng)前時(shí)間,作為要保存數(shù)據(jù)的文件名,以便區(qū)別文件及日后對(duì)此文件進(jìn)行操作。就如錄像文件,當(dāng)日保存下來的錄像肯定會(huì)用當(dāng)日的時(shí)間作為文件名稱的一部分,方便以后查看。
如:
Time t = new Time(); t.setToNow(); String filename = String.format("/record%04d%02d%02d%02d%02d%02d.avi", t.year, t.month+1 , t.monthDay, t.hour, t.minute, t.second);
音視頻項(xiàng)目中經(jīng)常會(huì)用此文件命名方式。
- Java字符串格式化功能?String.format用法詳解
- Java String.format()的用法
- Java之String.format()方法案例講解
- 快速入門介紹Java中強(qiáng)大的String.format()
- Java中String.format的使用方法總結(jié)
- JavaScript實(shí)現(xiàn)格式化字符串函數(shù)String.format
- JAVA字符串格式化-String.format()的使用
- C#中string.format用法詳解
- javascript模擬實(shí)現(xiàn)C# String.format函數(shù)功能代碼
- js中的string.format函數(shù)代碼
- javascript下string.format函數(shù)補(bǔ)充
- javascript下利用arguments實(shí)現(xiàn)string.format函數(shù)
- asp.net String.format中大括號(hào)的加入方法
- 為javascript添加String.Format方法
相關(guān)文章
c# WPF如何實(shí)現(xiàn)滾動(dòng)顯示的TextBlock
這篇文章主要介紹了c# WPF如何實(shí)現(xiàn)滾動(dòng)顯示的TextBlock,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03C#9.0新特性詳解——頂級(jí)程序語句(Top-Level Programs)
這篇文章主要介紹了C#9.0新特性詳解——頂級(jí)程序語句(Top-Level Programs)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2020-12-12C# DataTable 轉(zhuǎn)換為 實(shí)體類對(duì)象實(shí)例
如果你的實(shí)體類與數(shù)據(jù)庫表是完全一致的。上代碼:2013-04-04C#調(diào)用C++DLL傳遞結(jié)構(gòu)體數(shù)組的終極解決方案
這篇文章主要介紹了C#調(diào)用C++DLL傳遞結(jié)構(gòu)體數(shù)組的終極解決方案的相關(guān)資料,需要的朋友可以參考下2017-01-01