import mx.formatters.NumberFormatter; public class RMBBigTransForm { private static var NUM:String = "零壹贰叁肆伍陆柒捌玖"; private static var UNIT:String = "仟佰拾个"; private static var GRADEUNIT:String = "仟万亿兆"; private static var DOTUNIT:String = "角分厘"; private static var GRADE:int = 4; private static var SIGN:String = "¥"; private static var nf:NumberFormatter= new NumberFormatter(); public static function toBigAmts(amount:Number):String { return toBigAmt(amount); } public static function toBigAmt(amount:Number):String { var amt:String = nf.format(amount); var d:Number = new Number(amount); var dotPart:String = ""; //取小数位 var intPart:String = ""; //取整数位 var dotPos:int; if((dotPos = amt.indexOf('.')) != -1) { intPart = amt.substring(0, dotPos); dotPart = amt.substring(dotPos + 1); } else { //jqk 2011-08-03 修改 //intPart = amt; intPart = d.toString(); } if(intPart.length > 16) { //Alert.show("The amount is too big."); return ""; } var intBig:String = intToBig(intPart); var dotBig:String = dotToBig(dotPart); //以下代码稍做修改,现在是完美的代码啦! if ((dotBig.length == 0)&&(intBig.length != 0)) { return intBig + "元整"; } else if((dotBig.length == 0)&&(intBig.length == 0)) { return intBig + "零元"; } else if((dotBig.length != 0)&&(intBig.length != 0)) { return intBig + "元" + dotBig; } else { return dotBig; } } private static function dotToBig( dotPart:String):String { //得到转换后的大写(小数部分) var strRet:String = ""; for(var i:int=0; i<dotPart.length && i<3; i++) { var num:int; if((num =int(dotPart.substring(i,i+1))) != 0) strRet += NUM.substring(num,num+1) + DOTUNIT.substring(i,i+1); } return strRet; } private static function intToBig(intPart:String):String { //得到转换后的大写(整数部分) var grade:int; //级长 var result:String = ""; var strTmp:String = ""; //得到当级长 grade = intPart.length / GRADE; //调整级次长度 if(intPart.length % GRADE != 0) grade += 1; //对每级数字处理 for(var i:int = grade; i >= 1; i--) { strTmp = getNowGradeVal(intPart, i);//取得当前级次数字 result += getSubUnit(strTmp);//转换大写 result = dropZero(result);//除零 //加级次单位 if( i>1 ) //末位不加单位 //单位不能相连续 if(getSubUnit(strTmp)=="零零零零") { //jqk 2011-08-03 删除 //result += "零"+GRADEUNIT.substring(i - 1, i); } else { result += GRADEUNIT.substring(i - 1, i); } } return result; } private static function getNowGradeVal( strVal:String,grade:int):String { //得到当前级次的串 var rst:String=""; if(strVal.length <= grade * GRADE) rst = strVal.substring(0,strVal.length - (grade-1)*GRADE); else rst = strVal.substring(strVal.length - grade*GRADE,strVal.length - (grade-1)*GRADE); return rst; } private static function getSubUnit(strVal:String):String { //数值转换 var rst:String = ""; for(var i:int = 0;i< strVal.length; i++){ var s:String = strVal.substring(i,i+1); var num:int =new int(s); //Integer.parseInt(); if(num == 0) { //“零”作特殊处理 if(i != strVal.length) //转换后数末位不能为零 rst += "零"; } else { //If IntKey = 1 And i = 2 Then //“壹拾”作特殊处理 //“壹拾”合理 //Else rst += NUM.substring(num,num+1); //End If //追加单位 if(i != strVal.length-1 )//个位不加单位 rst += UNIT.substring(i+4-strVal.length,i+4-strVal.length+1); } } return rst; } private static function dropZero(strVal:String):String { //去除连继的“零” var strRst:String; var strBefore:String; //前一位置字符 var strNow:String; //现在位置字符 strBefore = strVal.substring(0,1); strRst = strBefore; for(var i:int= 1; i<strVal.length; i++) { strNow = strVal.substring(i, i+1); if(strNow=="零" && strBefore=="零") { //同时为零 } else strRst += strNow; strBefore = strNow; } //末位去零 if(strRst.substring(strRst.length-1, strRst.length)=="零") strRst = strRst.substring(0,strRst.length-1); return strRst; } }
?
以上代码为网上摘录,原文有错误,我修改部分代码,现在可以正确使用