用JavaScript将数字转换为大写金额
1 var digitUppercase = function(n) { 2 var fraction = ['角', '分']; 3 var digit = [ 4 '零', '壹', '贰', '叁', '肆', 5 '伍', '陆', '柒', '捌', '玖' 6 ]; 7 var unit = [ 8 ['元', '万', '亿'], 9 ['', '拾', '佰', '仟'] 10 ]; 11 var head = n < 0 ? '欠' : ''; 12 n = Math.abs(n); 13 var s = ''; 14 for (var i = 0; i < fraction.length; i++) { 15 s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, ''); 16 } 17 s = s || '整'; 18 n = Math.floor(n); 19 for (var i = 0; i < unit[0].length && n > 0; i++) { 20 var p = ''; 21 for (var j = 0; j < unit[1].length && n > 0; j++) { 22 p = digit[n % 10] + unit[1][j] + p; 23 n = Math.floor(n / 10); 24 } 25 s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s; 26 } 27 return head + s.replace(/(零.)*零元/, '元') 28 .replace(/(零.)+/g, '零') 29 .replace(/^整$/, '零元整'); 30 }; 31 32 console.log(digitUppercase(7682.01)); //柒仟陆佰捌拾贰元壹分 33 console.log(digitUppercase(7682)); //柒仟陆佰捌拾贰元整 34 console.log(digitUppercase(951434677682.00)); //玖仟伍佰壹拾肆亿叁仟肆佰陆拾柒万柒仟陆佰捌拾贰元整
转自:http://www.css88.com/
混在成都微信:unfunction