4.8 数字金额大写转换 插件 > 我的程序猿之路:第三十八章
1 <script> 2 3 /** 数字金额大写转换(可以处理整数,小数,负数) */ 4 function smalltoBIG(n) 5 { 6 var fraction = ['角', '分']; 7 var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; 8 var unit = [ ['元', '万', '亿'], ['', '拾', '佰', '仟'] ]; 9 var head = n < 0? '欠': ''; 10 n = Math.abs(n); 11 12 var s = ''; 13 14 for (var i = 0; i < fraction.length; i++) 15 { 16 s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, ''); 17 } 18 s = s || '整'; 19 n = Math.floor(n); 20 21 for (var i = 0; i < unit[0].length && n > 0; i++) 22 { 23 var p = ''; 24 for (var j = 0; j < unit[1].length && n > 0; j++) 25 { 26 p = digit[n % 10] + unit[1][j] + p; 27 n = Math.floor(n / 10); 28 } 29 s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s; 30 } 31 return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整'); 32 } 33 </script>
<script> var id = document.getElementById("id1"); document.getElementById("id2").value=smalltoBIG(id.value); </script> <INPUT TYPE="TEXT" ID="ID1"> <INPUT TYPE="TEXT" ID="ID2">