-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnzh.js
353 lines (337 loc) · 11.6 KB
/
nzh.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
待优化点
1.小数点拆分计算 1
2.代码简洁化 1
3.亿亿转万万亿 1
4.转金额规则规范化 参考 http://baike.baidu.com/link?url=lqGf7GrSnMPYvmrb4qMP_yS9k7aATfDidjsAC3eHv7Sxm76_hbamjuLaZH_g74n0Mr-a9CwIy6ekOIEK3Lt-G_
待补充点
1.中文数字转阿拉伯数字 1
2.港台地区支持 1
3.自定义转换 1
4.科学记数法字符串支持
*/
(function (name, factory) {
if (typeof process === "object" && typeof module === "object" && typeof module.exports) {
module.exports = factory();
} else if (typeof define === 'function' && (define.amd || define.cmd)) {
define([], factory);
} else if (typeof window !== "undefined" || typeof self !== "undefined") {
var global = typeof window !== "undefined" ? window : self;
global[name] = factory();
} else {
throw new Error("加载 " + name + " 模块失败!,请检查您的环境!")
}
}('Nzh', function () {
var langs = {
s:{
ch: '零一二三四五六七八九'
,ch_u: '个十百千万亿'
,other: '负点'
},
b:{
ch: '零壹贰叁肆伍陆柒捌玖'
,ch_u: '个拾佰仟万亿'
,other: '负点'
,m_t: '人民币'
,m_z: '整'
,m_u: '元角分'
},
s_hk:{
ch: '零一二三四五六七八九'
,ch_u: '個十百千萬億'
,other: '負點'
},
b_hk:{
ch: '零壹貳參肆伍陸柒捌玖'
,ch_u: '個拾佰仟萬億'
,other: '負點'
,m_t: '$'
,m_z: '整'
,m_u: '圓角分'
}
}
var regs = {
number : /^([+-])?0*(\d+)(\.(\d+))?$/
,number_e: /^([+-])?0*(\d+)(\.(\d+))?(e([+-])?(\d+))?$/ //科学计数法,下次实现
,is10to19 : /^1\d$/
}
function unshift0(arr,n){
if(n == null) n=1;
for(;n--;) arr.unshift(0);
}
function centerarr(barr,sarr){
for(var i=0;i<sarr.length;i++){
barr.splice(i,1,sarr[i]);
}
sarr.splice(0,sarr.length);
if(arguments.length > 2){
var r = [].slice.call(arguments,2);
r.unshift(barr);
centerarr.apply(null,r);
}
return barr;
}
function zero_comm(str,char_0,type){
str = str.split("");
if(!type || type=="$"){
for(var i=str.length;i--;){
if(str[i] == char_0){
str[i]=""
}else{
break;
}
}
}
if(!type || type=="nto1"){
var mark = false;
for(var i=0;i<str.length;i++){
if(str[i] == char_0){
if(mark) str[i] = "";
mark = true;
}else{
mark = false;
}
}
}
return str.join('');
}
function toCL(num,m,ww,dg){
var result = regs.number.exec(num.toString());
if(!result && typeof num == "number"){
//return '超出出范围!'
return num;
}else if(!result){
//return '参数错误!'
return num;
}
//var _ww = ww == null ? true : ww;
var ch = this.ch
,ch_u = this.ch_u
,ch_o = this.other
,n0 = ch.charAt(0)
,reg = new RegExp(n0+"*$")
,reg1 = new RegExp(n0+"+",'g')
,reg2 = new RegExp("0*$")
//,reg3 = new RegExp(ch_u.charAt(5)+'{2}')
var _int = result[2]
,_decimal = result[4]
,_minus = result[1] == "-";
var int = ""
,dicimal = ""
,minus = _minus ? ch_o.charAt(0) : ''; //符号位
//转换小数部分
if(_decimal){
_decimal = _decimal.replace(reg2,""); //去除尾部0
for(var x=0 ; x < _decimal.length ; x++){
dicimal += ch.charAt(+_decimal.charAt(x));
}
dicimal = dicimal ? ch_o.charAt(1) + dicimal : "";
}
//转换整数部分
//一位整数
if(_int.length == 1) return minus + ch.charAt(+_int) + dicimal;
//两位整数
if(m && _int.length == 2 && _int.charAt(0) === "1"){
//10的口语化处理
int = ch_u.charAt(1);
int += _int.charAt(1) == "0" ? "" : ch.charAt(+_int.charAt(1));
}else if(_int.length<=4){
//小于四位
for(var i=0,n=_int.length;n--;){
var _num = _int.charAt(i++);
int += ch.charAt(+_num) + (+_num && n?ch_u.charAt(n):'')
}
}else{
//大数递归
var d = _int.length/4>>0
,y = _int.length%4
,es = y || 4;
while (y==0 || !ch_u.charAt(3+d)){
y+=4;
d--;
}
int = toCL.call(this,_int.substr(0,y),m,ww,1) + ch_u.charAt(3+d)
+ (~(_int.substr(y-1,2).indexOf("0"))?n0:'')
+ toCL.call(this,_int.substr(y),false,ww,1)
}
//console.log(int);
//int = int.replace(reg1,n0).replace(reg,'');
int = zero_comm(int,n0);
// int = zero_comm(int,n0,'$');
if(!dg && ww && ch_u.length>5){
var dw_w = ch_u.charAt(4), dw_y = ch_u.charAt(5);
var lasty = int.lastIndexOf(dw_y);
if(~lasty){
int = int.substring(0,lasty).replace(new RegExp(dw_y,'g'),dw_w+dw_w) + int.substring(lasty);
}
}
return minus + int + dicimal;
}
function unCL(cnnumb){
var result = cnnumb.split(this.other.charAt(1));
var _int = result[0].replace(this.other.charAt(0),"")
,_decimal = result[1]
,_minus = !!~result[0].indexOf(this.other.charAt(0));
var dw_w = this.ch_u.charAt(4), dw_y = this.ch_u.charAt(5);
_int = _int.replace(new RegExp(dw_w+"{2}","g"),dw_y);
var cnarr = _int.split('');
var rnum=0,num=0,_num=0,dw=0,maxdw=0;
var rnum_a=[],num_a=[],_num_a=[];
function wei(u){
return u >= 5 ? (u-4)*4+4 : u;
}
for(var i=0;i<cnarr.length;i++){
var chr = cnarr[i];
var n=0,u=0;
if(~(n = this.ch.indexOf(chr))){
//_num = _num*10 + n;
_num_a.unshift(n);
}else if(~(u = this.ch_u.indexOf(chr))){
if(dw>u){//正常情况
// num += _num * (u == 5 ? Math.pow(10,8): Math.pow(10,u));
// _num = 0;
unshift0(_num_a,wei(u));
centerarr(num_a,_num_a);
}else if(u>=maxdw){//后跟大单位
// if(i==0) _num = 1;
// rnum += num + _num;
// rnum *= u == 5 ? Math.pow(10,8): Math.pow(10,u);
// num = 0;
// _num = 0;
maxdw = u;
if(i==0) _num_a=[1];
centerarr(rnum_a,num_a,_num_a);
unshift0(rnum_a,wei(u));
}else{
//num = (num + _num) * (u == 5 ? Math.pow(10,8): Math.pow(10,u));
//_num = 0;
dw = u;
centerarr(num_a,_num_a);
unshift0(num_a,wei(u));
}
}else{
//return cnnumb;
}
}
centerarr(rnum_a,num_a,_num_a).reverse();
var decimal = 0;
if(_decimal){
rnum_a.push('.')
decimal = '0.'
for(var i=0; i<_decimal.length; i++){
decimal+=this.ch.indexOf(_decimal.charAt(i));
rnum_a.push(this.ch.indexOf(_decimal.charAt(i)));
}
decimal = +decimal;
}
rnum = (rnum + num + _num + decimal)*(_minus?-1:1);
//console.log(rnum_a.join(''));
//console.log(rnum);
return rnum_a.join('');
}
function toMoney(num,ww){
//lang = (typeof lang == 'object' && lang.m_u) ? lang : langs.b;
var result = regs.number.exec(num.toString());
if(!result && typeof num == "number"){
//return '超出出范围!'
return num;
}else if(!result){
//return '参数错误!'
return num;
}
var _num = result.slice(1,3).join('')
,_decimal = result[4];
var xs_str = _decimal ? '' : this.m_z;
if(_decimal){
for(var i=0; i<this.m_u.length-1;i++){
xs_str += _decimal.charAt(i) == 0 ? '' : toCL.call(this,_decimal.charAt(i)) + this.m_u.charAt(i+1);
}
}
if(_num != "0" || xs_str == this.m_z || xs_str == ''){
return this.m_t + toCL.call(this,_num,null,ww) + this.m_u.charAt(0) + xs_str;
}else{
return this.m_t + xs_str;
}
}
var Nzh = function(lang){
this.lang = lang;
};
Nzh.prototype = {
encode:function(){return toCL.apply(this.lang,arguments)}
,decode:function(){return unCL.apply(this.lang,arguments)}
,toMoney:function(){return toMoney.apply(this.lang,arguments)}
}
Nzh.langs = langs;
Nzh._y2ww = true; //默认启用 "万万"
Nzh.cn = {
encodeS: function(num,m,ww){
return toCL.call(langs.s,num,(m == null ? true : m),(ww == null ? Nzh._y2ww : ww));
},
encodeB: function(num,m,ww){
return toCL.call(langs.b,num,m,(ww == null ? Nzh._y2ww : ww));
},
decodeS: function(str){
return unCL.call(langs.s,str);
},
decodeB: function(str){
return unCL.call(langs.b,str);
},
toMoney: function(num,ww){
return toMoney.call(langs.b,num,(ww == null ? Nzh._y2ww : ww));
}
};
Nzh.hk = {
encodeS: function(num,m,ww){
return toCL.call(langs.s_hk,num,(m == null ? true : m),(ww == null ? Nzh._y2ww : ww));
},
encodeB: function(num,m,ww){
return toCL.call(langs.b_hk,num,m,(ww == null ? Nzh._y2ww : ww));
},
decodeS: function(str){
return unCL.call(langs.s_hk,str);
},
decodeB: function(str){
return unCL.call(langs.b_hk,str);
},
toMoney: function(num,ww){
return toMoney.call(langs.b_hk,num,(ww == null ? Nzh._y2ww : ww));
}
};
//简体中文
// nzh.toS =
// nzh.encodeS = function(num,m,ww){
// return toCL.call(langs.s,num,(m == null ? true : m),ww);
// }
// nzh.toB =
// nzh.encodeB = function(num,m,ww){
// return toCL.call(langs.b,num,m,ww);
// }
// nzh.toMoney = function(num){
// return toMoney.call(langs.b,num);
// }
// nzh.unS =
// nzh.decodeS = function(str){
// return unCL.call(langs.s,str);
// }
// nzh.unB =
// nzh.decodeB = function(str){
// return unCL.call(langs.b,str);
// }
// //繁体中文
// nzh.encodeS_hk = function(num,m,ww){
// return toCL.call(langs.s_hk,num,(m == null ? true : m),ww);
// }
// nzh.encodeB_hk = function(num,m,ww){
// return toCL.call(langs.b_hk,num,m,ww);
// }
// nzh.toMoney_hk = function(num){
// return toMoney.call(langs.b_hk,num);
// }
// nzh.decodeS_hk = function(str){
// return unCL.call(langs.s_hk,str);
// }
// nzh.decodeB_hk = function(str){
// return unCL.call(langs.b_hk,str);
// }
return Nzh;
}));