0

我遇到空格問題。另外,在諸如54個數字之間需要破折號;然而,我正在破解數字,例如五萬。以字符串形式返回數字輸入Javascript

此外,輸出後我不需要空格,但我一直有問題輸出空格。

任何想法?

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

http://stackoverflow.com/questions/5529934/javascript-numbers-to-words – mplungjan

+0

所以兩個人發現了兩個重複。 – mplungjan

+0

我的問題不是重複的。我的問題是基於原始的答案,但編輯我需要幫助弄清楚如何解決小錯誤。 –

回答

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; 
+0

那麼問題是什麼?爲什麼不在一個片段中發佈正在運行的版本? – mplungjan

+0

我不知道如何。 –

+0

點擊編輯器中的<<> – mplungjan

0

能不能請你波紋管。

var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen ']; 
 
var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety']; 
 

 
function inWords (num) { 
 
    if ((num = num.toString()).length > 9) return 'overflow'; 
 
    n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/); 
 
    if (!n) return; var str = ''; 
 
    str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + '-' + a[n[1][1]]) + 'million ' : ''; 
 
    str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + '-' + a[n[2][1]]) + 'hundred ' : ''; 
 
    str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + '-' + a[n[3][1]]) + 'thousand ' : ''; 
 
    str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + '-' + a[n[4][1]]) + 'hundred ' : ''; 
 
    str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + '-' + a[n[5][1]]) + 'only ' : ''; 
 
    return str; 
 
} 
 

 
console.log(inWords(999999999)); 
 
console.log(inWords(1095));

複製表單Convert digits into words with JavaScript完成爲輸出的一些變化。

+0

我可以在控制檯上看到「九千九百九十九萬九千九百九十九萬一千九百九十九隻。 – Mitul

相關問題