Skip to content

Commit

Permalink
数字金额转繁体中文大写字符串
Browse files Browse the repository at this point in the history
  • Loading branch information
yanlele committed Apr 4, 2018
1 parent 818ce80 commit b827f87
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions 18年/18年计划书.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@
- [17、jquery获取表单数据](./3月/17、jquery获取表单数据/README.md)

- 4月
- [01、数字金额转繁体中文大写字符串](./4月/01、数字金额转繁体中文大写字符串/index.js)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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]]
}
}
});
}

let money = intToChinese(123);
console.log(money);
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
- [07、flex布局的学习](./18年/3月/09、flex布局的学习)
- [08、html表格合并单元格](./18年/3月/14、html表格合并单元格/index.html)
- [09、清除浮动的解决方案总结](./18年/3月/16、清除浮动的解决方案总结/README.md)
- [10、数字金额转繁体中文大写字符串](./18年/4月/01、数字金额转繁体中文大写字符串/index.js)


- vue
Expand Down

0 comments on commit b827f87

Please sign in to comment.