Flex中如何利用NumberFormatter类按照一定标准格式化数字的例子
By Minidxer | January 3, 2009
接下来的例子演示了Flex中如何利用NumberFormatter类,按照一定标准格式化数字。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white">
- <mx:Script>
- <![CDATA[
- import mx.collections.ArrayCollection;
- import mx.formatters.NumberBaseRoundType;
- private function button_click(evt:MouseEvent):void {
- textInput.errorString = "";
- numberFormatter.format(textInput.text);
- if (numberFormatter.error) {
- textInput.errorString = numberFormatter.error;
- }
- arrColl = new ArrayCollection();
- numberFormatter.rounding = NumberBaseRoundType.NEAREST;
- arrColl.addItem({type:numberFormatter.rounding,
- value:numberFormatter.format(textInput.text)});
- numberFormatter.rounding = NumberBaseRoundType.UP;
- arrColl.addItem({type:numberFormatter.rounding,
- value:numberFormatter.format(textInput.text)});
- numberFormatter.rounding = NumberBaseRoundType.DOWN;
- arrColl.addItem({type:numberFormatter.rounding,
- value:numberFormatter.format(textInput.text)});
- numberFormatter.rounding = NumberBaseRoundType.NONE;
- arrColl.addItem({type:numberFormatter.rounding,
- value:numberFormatter.format(textInput.text)});
- }
- ]]>
- </mx:Script>
- <mx:ArrayCollection id="arrColl" />
- <mx:NumberFormatter id="numberFormatter"
- precision="2"
- rounding="up" />
- <mx:ApplicationControlBar dock="true">
- <mx:Form styleName="plain">
- <mx:FormItem label="number:"
- direction="horizontal">
- <mx:TextInput id="textInput"
- text="2.0499"
- restrict="[0-9.-]"
- maxChars="6" />
- <mx:Button label="format"
- click="button_click(event);" />
- </mx:FormItem>
- </mx:Form>
- </mx:ApplicationControlBar>
- <mx:DataGrid id="dataGrid"
- dataProvider="{arrColl}"
- rowCount="4" />
- </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子
Topics:
Other |
1 Comment » |
Tags: format(), NumberBaseRoundType, NumberFormatter
要怎麼格式化成這樣的字串,不足補零?謝謝
000123