我遇到空格问题。此外,在诸如五十四之间的数字之间需要破折号;但是,我的数字落后于五万人。
另外,输出后我不需要空格,但是我一直有输出空格的问题。
有什么想法吗?
PS:我已经研究过Convert digits into words with JavaScript - 我希望我的版本有用。
function number2words(n) {
a = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen '];
b = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
c = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
if ((n = n.toString()).length > 9) return false;
num = ('000000000' + n).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
if (!num)
return;
var str = '';
dash = '-';
if (c[num[5][1]] == '')
dash = '';
if (n <= 19 && n != 0)
return c[n];
else if (n == 0)
return 'zero';
else if (n == 20 || n == 30 || n == 40 || n == 50 || n == 60 || n == 70 || n == 80 || n == 90)
return b[n[0]];
else
str += (num[1] != 0) ? (a[Number(num[1])] || b[num[1][0]] + '-' + a[num[1][1]]) + 'million ' : '';
str += (num[2] != 0) ? (a[Number(num[2])] || b[num[2][0]] + '-' + a[num[2][1]]) + 'hundred ' : '';
str += (num[3] != 0) ? (a[Number(num[3])] || b[num[3][0]] + '-' + a[num[3][1]]) + 'thousand ' : '';
str += (num[4] != 0) ? (a[Number(num[4])] || b[num[4][0]] + '-' + a[num[4][1]]) + 'hundred' : '';
str += (num[5] != 0) ? ((str != '') ? ' ' : '') + (a[Number(num[5])] || b[num[5][0]] + dash + a[num[5][1]]) : '';
console.log(str[str.length - 1], str.length);
if (str[str.length - 1] == " ") {
var sl = str.slice(0, str.length - 1);
return sl;
}
return str;
}
console.log(number2words(15007));
console.log(number2words(464097));
答案 0 :(得分:0)
请你试试看。
bytearray(b'\x03')
&#13;
复制表单Convert digits into words with JavaScript为您的输出完成一些更改。
答案 1 :(得分:0)
<强>解决强>
function number2words(n) {
a = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen '];
b = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
c = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
if ((n = n.toString()).length > 9) return false;
num = ('000000000' + n).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
if (!num)
return;
var str = '';
dash = '-';
if (c[num[5][1]] == '')
dash = '';
if (n <= 19 && n != 0)
return c[n];
else if (n == 0)
return 'zero';
else if (n == 20 || n == 30 || n == 40 || n == 50 || n == 60 || n == 70 || n == 80 || n == 90)
return b[n[0]];
else
str += (num[1] != 0) ? (a[Number(num[1])] || b[num[1][0]] + '-' + a[num[1][1]]) + 'million ' : '';
str += (num[2] != 0) ? (a[Number(num[2])] || b[num[2][0]] + '-' + a[num[2][1]]) + 'hundred ' : '';
str += (num[3] != 0) ? (a[Number(num[3])] || b[num[3][0]] + '-' + a[num[3][1]]) + 'thousand ' : '';
str += (num[4] != 0) ? (a[Number(num[4])] || b[num[4][0]] + '-' + a[num[4][1]]) + 'hundred' : '';
str += (num[5] != 0) ? ((str != '') ? ' ' : '') + (a[Number(num[5])] || b[num[5][0]] + dash + a[num[5][1]]) : '';
str = str.split(" ").join(' ');
str = str.split("-thousand").join(" thousand");
if(str[str.length-1] == " "){
var sl = str.slice(0, str.length-1);
return sl;
}
return str;