Flex中如何利用NumberFormatter类按照一定标准格式化数字的例子

By Minidxer | January 3, 2009

接下来的例子演示了Flex中如何利用NumberFormatter类,按照一定标准格式化数字。

让我们先来看一下Demo可以右键View Source或点击这里察看源代码):


下面是完整代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="vertical"
  4.         verticalAlign="middle"
  5.         backgroundColor="white">
  6.  
  7.     <mx:Script>
  8.         <![CDATA[
  9.             import mx.collections.ArrayCollection;
  10.             import mx.formatters.NumberBaseRoundType;
  11.  
  12.             private function button_click(evt:MouseEvent):void {
  13.                 textInput.errorString = "";
  14.                 numberFormatter.format(textInput.text);
  15.                 if (numberFormatter.error) {
  16.                     textInput.errorString = numberFormatter.error;
  17.                 }
  18.  
  19.                 arrColl = new ArrayCollection();
  20.  
  21.                 numberFormatter.rounding = NumberBaseRoundType.NEAREST;
  22.                 arrColl.addItem({type:numberFormatter.rounding,
  23.                         value:numberFormatter.format(textInput.text)});
  24.  
  25.                 numberFormatter.rounding = NumberBaseRoundType.UP;
  26.                 arrColl.addItem({type:numberFormatter.rounding,
  27.                         value:numberFormatter.format(textInput.text)});
  28.  
  29.                 numberFormatter.rounding = NumberBaseRoundType.DOWN;
  30.                 arrColl.addItem({type:numberFormatter.rounding,
  31.                         value:numberFormatter.format(textInput.text)});
  32.  
  33.                 numberFormatter.rounding = NumberBaseRoundType.NONE;
  34.                 arrColl.addItem({type:numberFormatter.rounding,
  35.                         value:numberFormatter.format(textInput.text)});
  36.             }
  37.         ]]>
  38.     </mx:Script>
  39.  
  40.     <mx:ArrayCollection id="arrColl" />
  41.  
  42.     <mx:NumberFormatter id="numberFormatter"
  43.             precision="2"
  44.             rounding="up" />
  45.  
  46.     <mx:ApplicationControlBar dock="true">
  47.         <mx:Form styleName="plain">
  48.             <mx:FormItem label="number:"
  49.                     direction="horizontal">
  50.                 <mx:TextInput id="textInput"
  51.                         text="2.0499"
  52.                         restrict="[0-9.-]"
  53.                         maxChars="6" />
  54.                 <mx:Button label="format"
  55.                         click="button_click(event);" />
  56.             </mx:FormItem>
  57.         </mx:Form>
  58.     </mx:ApplicationControlBar>
  59.  
  60.     <mx:DataGrid id="dataGrid"
  61.             dataProvider="{arrColl}"
  62.             rowCount="4" />
  63.  
  64. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

Topics: Other | 1 Comment » | Tags: , ,

你可能还对下列文章感兴趣:

One comment | Add One

  1. 小遊戲 - 04/30/2009 at 10:26 am

    要怎麼格式化成這樣的字串,不足補零?謝謝

    000123

Leave a Comment

Name(*):

E-Mail(*) :

Website :

Comments :

Search Posts