js转换数字和人民币

从网上扣下来的

cardNoShow:function(cardNo,split){
        var tmpCardNo=" ";
        if(!split){split=" ";}
        if(!cardNo || !cardNo.length){
            return cardNo;
        }
        var times=0;
        while(cardNo.length>4 && times<4){
            tmpCardNo=tmpCardNo+cardNo.substring(0,4)+split;
            cardNo=cardNo.substring(4);
            times++;
        }
        tmpCardNo=tmpCardNo+cardNo;
        return tmpCardNo;
    },
    /**
     * 将小写金额转换为大写金额
     * @param amt 小写金额
     */
    bigcashShow:(function(amt){
        if(amt == ""){
            return "";
        }
        var aNum = ["%u96F6","%u58F9","%u8d30","%u53c1","%u8086","%u4F0D","%u9646","%u67D2","%u634C","%u7396"];
        var HUNDREDMILLION = 0;
        var TENTHOUSAND = 1;
        var THOUSAND = 2;
        var HUNDRED = 3;
        var TEN = 4;
        var YUAN = 5;
        var JIAO = 6;
        var CENT = 7;
        var ZHENG = 8;
        var aUnit = new Array(9);
        aUnit[HUNDREDMILLION] = "%u4EBF";   //亿
        aUnit[TENTHOUSAND] = "%u4E07";      //万
        aUnit[THOUSAND] = "%u4EDF";         //仟
        aUnit[HUNDRED] = "%u4F70";          //佰
        aUnit[TEN] = "%u62FE";              //拾
        aUnit[YUAN] = "%u5143";             //元
        aUnit[JIAO] = "%u89D2";             //角
        aUnit[CENT] = "%u5206";             //分
        aUnit[ZHENG] = "%u6574";            //整

        function filterCharacter( filterString, filterChar ){
            if( filterString == null || filterString.length == 0 ){
                return null;
            }
            var i = 0;  
            for( ; i < filterString.length; i++ ){
                if( filterString.charAt( i ) != filterChar )
                    break;
            }
            var ret = filterString.substring( i, filterString.length );
            ret = (ret.length > 0) ? ret : null;
            return ret; 
        }
        function convertIntegerToChineseCash( cash ){
            var tempCash = "";
            var returnCash = "";
            if( cash == null || cash.length == 0 )
                return null;
            var totalLen = cash.length;
            var times = ((cash.length % 4) > 0) ? ( Math.floor(cash.length/4) + 1 ) : Math.floor(cash.length/4);    
            var remainder = cash.length % 4;
            var i = 0;  
            for( ; i < times; i++ ){
                if( i == 0 && (remainder > 0) ) {
                    tempCash = cash.substring( 0, remainder );
                }else {
                    if( remainder > 0 )
                        tempCash = cash.substring( remainder+(i-1)*4, remainder+i*4 );
                    else
                        tempCash = cash.substring( i*4, i*4+4 );
                }
                tempCash = convert4ToChinese( tempCash, false );
                returnCash += tempCash;
                if( tempCash != null && tempCash.length != 0 ) 
                    returnCash += getUnit( times - i );
            }
            return returnCash;
        }
        function convertDecimalToChineseCash( cash, bOmitBeginZero ){
            var i = 0;
            var bBeginZero = false;
            var bMetZero = false;
            var returnCash = "";
            if( cash == null || cash.length == 0 )
                return returnCash;
            for( ; i < cash.length; i++ ){
                if( i >= 2 )
                    break;
                if( i == 0 && bOmitBeginZero && cash.charAt(i) == '0' ){
                    bBeginZero = true;
                    continue;
                }
                if( bBeginZero && cash.charAt(i) == '0' )
                    continue;
                if( cash.charAt(i) != '0' ) {
                    if( bMetZero )
                        returnCash += aNum[0]; //"零"
                    bMetZero = false;
                    returnCash += convert( cash.charAt(i) );
                    switch( i )
                    {
                    case 0:
                        returnCash += aUnit[JIAO]; //"角"
                        break;
                    case 1:
                        returnCash += aUnit[CENT]; //"分"
                        break;
                    default:
                        break;              
                    }
                }else {
                    bMetZero = true;
                }
            }
            return returnCash;  
        }
        function convert4ToChinese( cash, bOmitBeginZero ){
            var i = 0;
            var length = cash.length;
            var bBeginZero = false;
            var bMetZero = false;
            var returnCash = "";    

            for( ; i < length; i++ )
            {
                if( i == 0 && bOmitBeginZero && cash.charAt(i) == '0' )
                {
                    bBeginZero = true;
                    continue;
                }
                if( bBeginZero && cash.charAt(i) == '0' )
                    continue;

                if( cash.charAt(i) != '0' ) {
                    if( bMetZero )
                        returnCash += aNum[0]; //"零"
                    bMetZero = false;
                    returnCash += convert( cash.charAt(i) );
                    switch( i + (4-length) )
                    {
                    case 0:
                        returnCash += aUnit[THOUSAND]; //"千"
                        break;
                    case 1:
                        returnCash += aUnit[HUNDRED]; //"佰"
                        break;
                    case 2:
                        returnCash += aUnit[TEN]; //"拾"
                        break;
                    case 3:
                        returnCash += "";
                        break;
                    default:
                        break;              
                    }
                }else {
                    bMetZero = true;
                }
            }
            return returnCash;
        }
        function getUnit( part ){
            var returnUnit = "";
            var i = 0;

            switch( part )
            {
            case 1:
                returnUnit = "";
                break;
            case 2:
                returnUnit = aUnit[TENTHOUSAND]; // "万"
                break;
            case 3:
                returnUnit = aUnit[HUNDREDMILLION]; //"亿"
                break;
            default:
                if( part > 3 ){
                    for( ; i < part - 3; i++ ){
                        returnUnit += aUnit[TENTHOUSAND]; // "万"
                    }
                    returnUnit += aUnit[HUNDREDMILLION]; //"亿"
                }

                break;
            }
            return returnUnit;
        }
        function convert( num ){
            return aNum[parseInt(num)];
        }
        return function(cash){
            var integerCash="";
            var decimalCash=""; 
            var integerCNCash = "";
            var decimalCNCash = "";
            var dotIndex = 0;
            var cnCash = "";
            var Cash = "";
            Cash = $.trim( cash );
            if( Cash == null || Cash.length == 0 ){
                return cnCash;
            }

            if( !Util.isMoney( Cash ) ){
                return cnCash;  
            }
            dotIndex = Cash.indexOf('.');
            if( dotIndex != -1 ) {
                integerCash = Cash.substring( 0, dotIndex );
                decimalCash = Cash.substring( dotIndex + 1 );   
            }else {
                integerCash = Cash;
                decimalCash = null;
            }
            integerCNCash = filterCharacter( integerCash, '0' );
            if( integerCNCash == null ){
                integerCNCash = "";
            }else{
                integerCNCash = convertIntegerToChineseCash( integerCNCash );
            }
                decimalCNCash = convertDecimalToChineseCash( decimalCash, false );

            if( decimalCNCash == null || decimalCNCash.length == 0 ){
                if( integerCNCash == null || integerCNCash.length == 0 ){
                    cnCash = aNum[0] + aUnit[YUAN] + aUnit[ZHENG]; //"零元整"
                }else{
                    cnCash = integerCNCash + aUnit[YUAN] + aUnit[ZHENG]; //"元整"
                }
            }else {
                if( integerCNCash == null || integerCNCash.length == 0 ){
                    cnCash = decimalCNCash;
                }else{
                    cnCash = integerCNCash + aUnit[YUAN] + decimalCNCash;  //"元"
                }
            }
            var retCash = unescape(cnCash);

            if(cash<0.1 && cash!='0' &&cash!='0.'&&cash!='0.0'&&cash!='0.00'){
                    retCash = retCash.substring(1);
            }

            return retCash; 
        };
    })(),
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值