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)日保存下來(lái)的錄像肯定會(huì)用當(dāng)日的時(shí)間作為文件名稱(chēng)的一部分,方便以后查看。
如:
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ì)用此文件命名方式。
上一篇:C#利用反射技術(shù)實(shí)現(xiàn)去掉按鈕選中時(shí)的邊框效果
欄 目:C#教程
本文標(biāo)題:String.Format大全(C# Java)
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6997.html
您可能感興趣的文章
- 01-10淺析JAVA中過(guò)濾器、監(jiān)聽(tīng)器、攔截器的區(qū)別
- 01-10C#實(shí)現(xiàn)將javascript文件編譯成dll文件的方法
- 01-10C# 調(diào)用 JavaWebservice服務(wù)遇到的問(wèn)題匯總
- 01-10C#利用delegate實(shí)現(xiàn)Javascript的each方法
- 01-10舉例說(shuō)明Java多線程編程中讀寫(xiě)鎖的使用
- 01-10mvc C# JavaScript LigerUI oracle實(shí)現(xiàn)用戶的注冊(cè)、登陸驗(yàn)證、登陸
- 01-10C#編寫(xiě)ActiveX網(wǎng)頁(yè)截圖控件
- 01-10一種c#深拷貝方式完勝java深拷貝(實(shí)現(xiàn)上的對(duì)比分析)
- 01-10C#調(diào)用Java方法實(shí)例詳解
- 01-10C#和Java有什么區(qū)別和聯(lián)系


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文