日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用MessageFormat格式化数字,日期

發布時間:2024/4/17 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用MessageFormat格式化数字,日期 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.cppblog.com/biao/archive/2010/12/01/135119.html

如數字上加逗號,保留小數點后面兩位(會自動四舍五入),百分比,貨幣等。

參考實例:?http://www.java2s.com/Code/Java/Development-Class/MessageFormat.htm

import java.text.MessageFormat;


public class Test {

? ? public static void main(String[] args) {

? ? ? ? Object[] params = { new Integer(123), new Double(1222234.567) };

? ? ? ? String msg = MessageFormat.format("{0,number,percent} and {1,number,,###.##}", params);

? ? ? ? System.out.println(msg);

? ? }

}

=========

http://sunxboy.iteye.com/blog/273950

第一個例子使用靜態的方法 MessageFormat.format ,它在內部創建一個只使用一次的 MessageFormat :

int planet = 7;String event = "a disturbance in the Force";String result = MessageFormat.format("At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",planet, new Date(), event);

輸出為:

At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.

下面的例子創建了一個可以重復使用的 MessageFormat 實例:

int fileCount = 1273;String diskName = "MyDisk";Object[] testArgs = {new Long(fileCount), diskName};MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0} file(s).");System.out.println(form.format(testArgs));

不同 fileCount 值的輸出:

The disk "MyDisk" contains 0 file(s).The disk "MyDisk" contains 1 file(s).The disk "MyDisk" contains 1,273 file(s).

對于更復雜的模式,可以使用 ChoiceFormat 來生成正確的單數和復數形式:

MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");double[] filelimits = {0,1,2};String[] filepart = {"no files","one file","{0,number} files"};ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);form.setFormatByArgumentIndex(0, fileform);int fileCount = 1273;String diskName = "MyDisk";Object[] testArgs = {new Long(fileCount), diskName};System.out.println(form.format(testArgs));

不同的 fileCount 值的輸出:

?

The disk "MyDisk" contains no files.The disk "MyDisk" contains one file.The disk "MyDisk" contains 1,273 files.

?

?

=============================================================

?

Java代碼 ?
  • MessageFormat?使用以下形式的模式: ??
  • ??
  • MessageFormatPattern: ??
  • String ??
  • MessageFormatPattern?FormatElement?String ??
  • ??
  • FormatElement: ??
  • {?ArgumentIndex?} ??
  • {?ArgumentIndex?,?FormatType?} ??
  • {?ArgumentIndex?,?FormatType?,?FormatStyle?} ??
  • ??
  • FormatType:?one?of ??
  • number?date?time?choice ??
  • ??
  • FormatStyle: ??
  • short??
  • medium ??
  • long??
  • full ??
  • integer ??
  • currency ??
  • percent ??
  • SubformatPattern ??
  • ??
  • String: ??
  • StringPartopt ??
  • String?StringPart ??
  • ??
  • StringPart: ??
  • ''??
  • '?QuotedString?'??
  • UnquotedString ??
  • ??
  • SubformatPattern: ??
  • SubformatPatternPartopt ??
  • SubformatPattern?SubformatPatternPart ??
  • ??
  • SubFormatPatternPart: ??
  • '?QuotedPattern?'??
  • UnquotedPattern??
  • MessageFormat 使用以下形式的模式:MessageFormatPattern: String MessageFormatPattern FormatElement StringFormatElement: { ArgumentIndex } { ArgumentIndex , FormatType } { ArgumentIndex , FormatType , FormatStyle }FormatType: one of number date time choiceFormatStyle: short medium long full integer currency percent SubformatPatternString: StringPartopt String StringPartStringPart: '' ' QuotedString ' UnquotedStringSubformatPattern: SubformatPatternPartopt SubformatPattern SubformatPatternPartSubFormatPatternPart: ' QuotedPattern ' UnquotedPattern

    ?

    在 String 中,"''" 表示單引號。QuotedString 可以包含除單引號之外的任意字符;圍繞的單引號被移除。UnquotedString 可以包含除單引號和左花括號之外的任意字符。因此,格式化后消息字符串為 "'{0}'" 的字符串可以寫作 "'''{'0}''" 或 "'''{0}'''"。

    在 SubformatPattern 中,應用了不同的規則。QuotedPattern 可包含除單引號之外的任意字符,但不移除圍繞的單引號,因此它們可以由子格式解釋。例如,"{1,number,$'#',##}" 將產生一個帶井號的數字格式,結果如:"$#31,45"。 UnquotedPattern 可以包含除單引號之外的任意字符,但其中的花括號必須成對出現。例如,"ab {0} de" 和 "ab '}' de" 是有效的子格式模式,而 "ab {0'}' de" 和 "ab } de" 則是無效的。

    http://terryjs.iteye.com/blog/768872

    ==================

    一。

    MessageFormat 提供了以與語言無關方式生成連接消息的方式。使用此方法構造向終端用戶顯示的消息。

    MessageFormat 獲取一組對象,格式化這些對象,然后將格式化后的字符串插入到模式中的適當位置。

    注: MessageFormat 不同于其他 Format 類,因為 MessageFormat 對象是用其構造方法之一創建的(而不是使用 getInstance MessageFormat 本身不實現特定于語言環境的行為。特定于語言環境的行為是由所提供的模式和用于已插入參數的子格式來定義的。

    ?

    ?

    MessageFormat運行開發者輸出文本中的變量的格式。它是一個強大的類,就像下面的例子展示的那樣:

    Java代碼 String?message?= ??
  •   "Once?upon?a?time?({1,date},?around?about?{1,time,short}),?there?"?+ ??
  •   "was?a?humble?developer?named?Geppetto?who?slaved?for?"?+ ??
  •   "{0,number,integer}?days?with?{2,number,percent}?complete?user?"?+ ??
  •   "requirements.?"; ??
  •   Object[?]?variables?=?new?Object[?] ??
  •   {?new?Integer(4),?new?Date(?),?new?Double(0.21)?} ??
  •   String?output?=?MessageFormat.format(?message,?variables?); ??
  •   System.out.println(output);???
  • String message ="Once upon a time ({1,date}, around about {1,time,short}), there " +"was a humble developer named Geppetto who slaved for " +"{0,number,integer} days with {2,number,percent} complete user " +"requirements. ";Object[ ] variables = new Object[ ]{ new Integer(4), new Date( ), new Double(0.21) }String output = MessageFormat.format( message, variables );System.out.println(output);

    ?

      隱藏在信息中的是描述輸出的格式的一種短小的代碼,范例的輸出如下:
      
      Once upon a time (Nov 3, 2002, around about 1:35 AM), there was a humble developer
      named Geppetto who slaved for 4 days with 21% complete user requirements.
      假如相同的信息需要被重復輸出但是變量的值不同,那么創建一個MessageFormat對象并給出信息。下面是上面的例子的修正版:
      

    Java代碼 //String?output?=?MessageFormat.format(message,?variables?); ??
  •   //變為: ??
  •   MessageFormat?formatter?=?new?MessageFormat(message); ??
  •   String?output?=?formatter.format(variables);???
  • //String output = MessageFormat.format(message, variables );//變為:MessageFormat formatter = new MessageFormat(message);String output = formatter.format(variables);

    ?

      
      除了可以處理日期、時間、數字和百分數外,MessageFormat也可以處理貨幣,運行更多的數字格式的控制并且答應指定ChoiceFormat。
      
      MessageFormat是一個極好的類,它應該經常被使用但是現在還沒有。它的最大的缺點是數據是被作為變量傳遞而不是一個Properties對象。一個簡單的解決辦法是寫一個封裝類,它會預解析字符串為格式化的結果,將Properties的key轉換為一個數組索引,順序是 Properties.keys( )返回的順序。

    ?

    二。例子

    <!-- Generated by javadoc (build 1.6.0-beta2) on Fri Mar 09 12:51:16 CST 2007 -->

    第一個例子 使用靜態的方法 MessageFormat.format ,它在內部創建一個只使用一次的 MessageFormat :

    Java代碼 int?planet?=?7; ??
  • ?String?event?=?"a?disturbance?in?the?Force"; ??
  • ??
  • ?String?result?=?MessageFormat.format( ??
  • ?????"At?{1,time}?on?{1,date},?there?was?{2}?on?planet?{0,number,integer}.", ??
  • ?????planet,?new?Date(),?event);??
  • int planet = 7;String event = "a disturbance in the Force";String result = MessageFormat.format("At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",planet, new Date(), event);

    ?

    輸出為:

    At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.

    下面的例子創建了一個可以重復使用的 MessageFormat 實例:

    Java代碼 int?fileCount?=?1273; ??
  • ?String?diskName?=?"MyDisk"; ??
  • ?Object[]?testArgs?=?{new?Long(fileCount),?diskName}; ??
  • ??
  • ?MessageFormat?form?=?new?MessageFormat( ??
  • ?????"The?disk?\"{1}\"?contains?{0}?file(s)."); ??
  • ??
  • ?System.out.println(form.format(testArgs));??
  • int fileCount = 1273;String diskName = "MyDisk";Object[] testArgs = {new Long(fileCount), diskName};MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0} file(s).");System.out.println(form.format(testArgs));

    ?

    不同 fileCount 值的輸出:

    The disk "MyDisk" contains 0 file(s).The disk "MyDisk" contains 1 file(s).The disk "MyDisk" contains 1,273 file(s).

    對于更復雜的模式,可以使用 ChoiceFormat 來生成正確的單數和復數形式:

    Java代碼 MessageFormat?form?=?new?MessageFormat("The?disk?\"{1}\"?contains?{0}."); ??
  • double[]?filelimits?=?{0,1,2}; ??
  • String[]?filepart?=?{"no?files","one?file","{0,number}?files"}; ??
  • ChoiceFormat?fileform?=?new?ChoiceFormat(filelimits,?filepart); ??
  • form.setFormatByArgumentIndex(0,?fileform); ??
  • ??
  • int?fileCount?=?1273; ??
  • String?diskName?=?"MyDisk"; ??
  • Object[]?testArgs?=?{new?Long(fileCount),?diskName}; ??
  • ??
  • System.out.println(form.format(testArgs));??
  • MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");double[] filelimits = {0,1,2};String[] filepart = {"no files","one file","{0,number} files"};ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);form.setFormatByArgumentIndex(0, fileform);int fileCount = 1273;String diskName = "MyDisk";Object[] testArgs = {new Long(fileCount), diskName};System.out.println(form.format(testArgs));

    ?

    不同的 fileCount 值的輸出:

    The disk "MyDisk" contains no files.The disk "MyDisk" contains one file.The disk "MyDisk" contains 1,273 files.

    如上例所示,可以以編程方式來創建 ChoiceFormat ,或使用模式創建。有關更多信息,請參閱 ChoiceFormat 。

    Java代碼 form.applyPattern( ??
  • ???"There?{0,choice,0#are?no?files|1#is?one?file|1<are?{0,number,integer}?files}.");??
  • form.applyPattern("There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.");

    ?

    注: 從上面的例子可以看到,由 MessageFormat 中的 ChoiceFormat 所生成的字符串要進行特殊處理;'{' 的出現用來指示子格式,并導致遞歸。如果 MessageFormat 和 ChoiceFormat 都是以編程方式創建的(而不是使用字符串模式),那么要注意不要生成對其自身進行遞歸的格式,這將導致無限循環。

    當一個參數在字符串中被多次解析時,最后的匹配將是解析的最終結果。例如,

    Java代碼 MessageFormat?mf?=?new?MessageFormat("{0,number,#.##},?{0,number,#.#}"); ??
  • Object[]?objs?=?{new?Double(3.1415)}; ??
  • String?result?=?mf.format(?objs?); ??
  • //?result?now?equals?"3.14,?3.1" ??
  • objs?=?null; ??
  • objs?=?mf.parse(result,?new?ParsePosition(0)); ??
  • //?objs?now?equals?{new?Double(3.1)}??
  • MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");Object[] objs = {new Double(3.1415)};String result = mf.format( objs );// result now equals "3.14, 3.1"objs = null;objs = mf.parse(result, new ParsePosition(0));// objs now equals {new Double(3.1)}

    ?

    同樣,使用包含同一參數多個匹配項的模式對 MessageFormat 對象進行解析時將返回最后的匹配。例如,

    Java代碼 MessageFormat?mf?=?new?MessageFormat("{0},?{0},?{0}"); ??
  • ?String?forParsing?=?"x,?y,?z"; ??
  • ?Object[]?objs?=?mf.parse(forParsing,?new?ParsePosition(0)); ??
  • ?//?result?now?equals?{new?String("z")}??
  • MessageFormat mf = new MessageFormat("{0}, {0}, {0}");String forParsing = "x, y, z";Object[] objs = mf.parse(forParsing, new ParsePosition(0));// result now equals {new String("z")}

    ?

    ?

    同步

    消息格式不是同步的。建議為每個線程創建獨立的格式實例。如果多個線程同時訪問一個格式,則它必須是外部同步的。

    ?

    總結

    以上是生活随笔為你收集整理的使用MessageFormat格式化数字,日期的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。