JavaScript实现将多位数转换成中文

GitHub地址:https://github.com/funnyGH/angular-pipe

var map_obj = {
" 1 " : " " ,
" 2 " : " " ,
" 3 " : " " ,
" 4 " : " " ,
" 5 " : " " ,
" 6 " : " " ,
" 7 " : " " ,
" 8 " : " " ,
" 9 " : " "
};

var units = [ "" , " " , " " , " "] ;

function prveIs0 ( sub_str , index ) {
if ( index == 0) return false ;
return sub_str . substring( index - 1 , index) == " 0 " ;
}

function unitIndexWithIndex ( sub_str , index ) {
return sub_str . length - index - 1 ;
}

function unitWithIndex ( sub_str , index ) {
if ( index == sub_str . length - 1) return "" ;
return units[ unitIndexWithIndex( sub_str , index)] ;
}

function bigUnitWithIndex ( index , count ) {
var bigUnits = [] ;
if ( count == 3) {
bigUnits = [ "" , " " , " 亿 "] ;
}
if ( count == 2) {
bigUnits = [ "" , " "] ;
}
if ( count == 0) {
return "" ;
}
return bigUnits[ bigUnits . length - index - 1] ;
}

function subParseChineseWord ( result_str , sub_num_str ) {
var sub_result = "" ;
for ( let index = 0 ; index < sub_num_str . length ; index ++) {
const element = sub_num_str . substring( index , index + 1) ;
if ( element == " 0 ") {
continue ;
}
if ( prveIs0( sub_num_str , index)) {
sub_result = sub_result . concat( " ") ;
}
sub_result = sub_result . concat( map_obj[ element]) . concat( unitWithIndex( sub_num_str , index)) ;
}
if ( sub_result . length == 0) {
if ( result_str . length > 0 && result_str . substr( result_str . length - 1 , 1) == " ") {
return "" ;
}
sub_result = " " ;
} else {
if ( sub_result . substr( 0 , 1) == " " && ( result_str . length > 0 && result_str . substr( result_str . length - 1 , 1) == " ")) {
return sub_result . substr( 1 , sub_result . length - 1) ;
}
}
return sub_result ;
}

function divisionNumStr ( num_str ) {
var list = new Array() ;
var origin_count = num_str . length / 4 ;
var count = parseInt( origin_count) ;
var begin_length = 0 ;
if ( origin_count !== count) {
begin_length = num_str . length - count * 4 ;
list . push( num_str . substr( 0 , begin_length)) ;
}
for ( let index = 0 ; index < count ; index ++) {
list . push( num_str . substr( index * 4 + begin_length , 4)) ;
}
return list ;
}

function parseChineseWord ( number ) {
var num_str = String( number) ;
var list = divisionNumStr( num_str) ;
var result = "" ;
list . forEach( function ( e , index , array ) {
var sub_str = subParseChineseWord( result , e) ;
if ( sub_str != " ") {
sub_str = sub_str . concat( bigUnitWithIndex( index , array . length)) ;
}
result = result . concat( sub_str) ;
}) ;
return result ;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值