表格中展示的比率,對比率的處理是:保留兩位小數,并向上保留。通過對某列的值進行數字格式化來實現保留兩位小數
1、問題背景
一般的,表格中展示的比率,對比率的處理是:保留兩位小數,并向上保留
2、實現實例
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" fontSize="12" fontFamily="微軟雅黑"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Script> <![CDATA[ import mx.collections.ArrayCollection; import mx.events.FlexEvent; [Bindable] //表格數據綁定 private var gridArray:ArrayCollection = new ArrayCollection([ {week:"星期一",apple:"3676",rate:"0.7868"}, {week:"星期二",apple:"4534",rate:"0.65454"}, {week:"星期三",apple:"6758",rate:"0.876454"}, {week:"星期四",apple:"9808",rate:"0.34224"}, {week:"星期五",apple:"6567",rate:"0.9876523"}, {week:"星期六",apple:"9000",rate:"0.566777"}, {week:"星期日",apple:"4533",rate:"0.988787"} ]); /** * 對表格中的比率進行格式化 */ private function formatDataGrid(item:Object,column:DataGridColumn):String { var temp:Number = item.rate; var data:String = dataFormatter.format(temp); return data; } ]]> </fx:Script> <fx:Declarations> <!--格式化數字--> <mx:NumberFormatter id="dataFormatter" precision="2" rounding="up"/> </fx:Declarations> <mx:VBox width="100%" height="100%" paddingBottom="10" paddingLeft="10" paddingRight="20" paddingTop="10" horizontalAlign="center"> <mx:DataGrid id="dataGrid" width="100%" height="90%" dataProvider="{gridArray}" textAlign="center"> <mx:columns> <mx:DataGridColumn headerText="星期" dataField="week"/> <mx:DataGridColumn headerText="蘋果" dataField="apple"/> <mx:DataGridColumn headerText="比率" dataField="rate" labelFunction="formatDataGrid"/> </mx:columns> </mx:DataGrid> </mx:VBox> </s:Application>