Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
```
function getCode(num) {
num = (num + '').split('');
var map = {0: '', 1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六', 7: '七', 8: '八', 9: '九', 10: '十'};
var _map = {0: '', 1: '十', 2: '百', 3: '千', 4: '万'};
var now;
var index = 0;
var re = [];
while((now = num.pop())) {
if (+now) {
re.unshift(map[now] + _map[index]);
}
else if (index) {
re.unshift('零');
}
index++;
}
return re.join('').replace(/^一十/, '十').replace(/零+/, '零').replace(/零$/, '');
}
// angular封装filter
angular.mobule('app').filter('toCN', function () {
var filter = function (num) {
num = (num + '').split('');
var map = {0: '', 1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六', 7: '七', 8: '八', 9: '九', 10: '十'};
var _map = {0: '', 1: '十', 2: '百', 3: '千', 4: '万'};
var now;
var index = 0;
var re = [];
while((now = num.pop())) {
if (+now) {
re.unshift(map[now] + _map[index]);
}
else if (index) {
re.unshift('零');
}
index++;
}
return re.join('').replace(/^一十/, '十').replace(/零+/, '零').replace(/零$/, '');
};
return filter;
});
```
```
// 用法
<div>{{test | toCH}}</div>
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment