JavaScript将数字转换成大写中文的方法
本文实例讲述了JavaScript将数字转换成大写中文的方法。分享给大家供大家参考。具体实现方法如下:
function intToChinese ( str ) { str = str+''; var len = str.length-1; var idxs = ['','十','百','千','万','十','百','千','亿','十','百','千','万','十','百','千','亿']; var num = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖']; return str.replace(/([1-9]|0+)/g,function( $, $1, idx, full) { var pos = 0; if( $1[0] != '0' ){ pos = len-idx; if( idx == 0 && $1[0] == 1 && idxs[len-idx] == '十'){ return idxs[len-idx]; } return num[$1[0]] + idxs[len-idx]; } else { var left = len - idx; var right = len - idx + $1.length; if( Math.floor(right/4) - Math.floor(left/4) > 0 ){ pos = left - left%4; } if( pos ){ return idxs[pos] + num[$1[0]]; } else if( idx + $1.length >= len ){ return ''; }else { return num[$1[0]] } } }); }
希望本文所述对大家的javascript程序设计有所帮助。
相关推荐
巴黎的妖 2019-04-25
angqiuli 2019-12-28
codeAB 2019-12-02
galoisgodel 2012-01-15
shishengsoft 2019-09-02
Jruing 2013-05-05
sovinchan 2019-06-27
hbzhouyj 2011-12-28
Dansha的花果山 2011-05-18
Huangguohao 2019-06-13
hyfound 2018-10-18
sihua 2019-02-27
Code 2019-02-18
weimingjun0 2017-08-23
xcxvbf 2019-01-16
海内存知己 2015-07-29