//数字转英文 const arr1 = ["zero","one","two","three","four","five","six","seven","eight","nine"], arr2 = ["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"], arr3 = ["tewnty","thirty","forty","fifty","sixty","seventy","eighty","ninety"], arr4 = ["hundred","thousand","million","billion","trillion","quadrillion"]; function doubledight(n){ var result = ""; if(parseInt(n)!=0){ var dd = n.split(''); if(dd[0]<1){ result = arr1[dd[1]]; }else if(dd[0]==1){ result = arr2[dd[1]]; }else{ result = arr3[dd[0]-2] + "-" + arr1[dd[1]]; } } return result; } function num2Eng(s){ s = s.split(''); while(s.indexOf('0')===0){ s.shift(); } s = s.join(''); var res = ''; while(s.length>3){ res = ',' + s.slice(-3) + res; s = s.slice(0,s.length-3); } if(s){ res = s + res; } var fenge = res.split(','); var result = ""; for(let i=0;i<fenge.length;i++){ if(fenge[i].length === 3){ result += arr1[fenge[i].substring(0,1)]+" ";//百位 result += arr4[0]; if(doubledight(fenge[i].substring(1))!=""){ result += " and " + doubledight(fenge[i].substring(1)); } }else if(fenge[i].length ==2){ result +=doubledight(fenge[i]); }else if(fenge[i].length == 1){ result += arr1[fenge[i]]; } if(i<fenge.length -1){ result += " " +arr4[fenge.length-1-i] + " "; } } return result; } var s1 = '2356'; console.log(num2Eng(s1)); var s2 = '9875538376'; console.log(num2Eng(s2)); var s3 = '00000002356'; console.log(num2Eng(s3));
全部评论
(0) 回帖