欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Flex中对表格某列的值进行数字格式化并求百分比添加%

程序员文章站 2022-07-10 15:10:03
1、问题背景 一般的,需要对表格中某列的数值进行格式化,对该数值乘以100,并保留两位小数,添加“%” 2、实现实例

1、问题背景

一般的,需要对表格中某列的数值进行格式化,对该数值乘以100,并保留两位小数,添加“%”

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 tempdata:number = item.rate*100; 
var data:string = dataformatter.format(tempdata); 
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>

(1)将小数乘以100

var tempdata:number = item.rate*100;

(2)对数值格式化,保留两位小数

var data:string = dataformatter.format(tempdata);

(3)添加百分号

return data+"%";

3、实现结果
Flex中对表格某列的值进行数字格式化并求百分比添加%

相关标签: 格式化 百分比