面试题之:数字转换

 

有一个数字,比如 1234, 写一个函数把它转成它的英文写法,上面这个应该输出 one thousand two hundred and thirty four

英文翻译:

  0 - 9 : zero, one ……

  10 - 19 : ten, eleven……

  20 - 100: twenty, twenty one……

  100:  one hundred

  111 :  one hundred and eleven

  121 :  one hundred and twenty one

  1015:  one thousand and fifteen

     100 001:   one hundred thousand and one

  121886:  One hundred and twenty-one thousand eight hundred and eighty-six

 

代码如下:<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .result{
            margin-top: 30px;
            padding: 20px 10px;
            border: 1px solid #999;
            background-color: beige
        }
    </style>
</head>
<body>
   <p>给你1234,
    <br/>输出 one thousand two hundred and thirty four</p>

   <input id="num" type="number" />
   <button onclick="setEnglish()">生成</button>
   <div class="result" id="result"></div>
</body>
<script>

    const arr1 = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'],
        arr2 = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineTeen'],
        arr3 = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'],
        arr4 = ['hundred', 'thousand', 'million', 'billion', 'trillion', 'quadrillion'];
    function setEnglish(){
        let num = document.getElementById('num').value
     if(!num){
            alert('请输入内容')
        }
        let res = this.numToEnglish(num)
        document.getElementById('result').innerText = res
    }

    function numToEnglish(num){
        strnum = num.toString()  // 这一个需要用到

        if(num >= 0 && num < 10){ 
            return arr1[num]
        }else if(num >= 10 && num < 20){
            let key = strnum.substring(1)
            return arr2[key]
        }else if(num >= 20 && num < 100){
            let s = strnum.substring(0,1)-2  // 十位
            let g = strnum.substring(1) // 各位
            return `${arr3[s]}${g > 0 ? ' '+arr1[g] : '' }`
        }else if(num >= 100 && num <1000){
            let b = arr1[strnum.substring(0,1)] + " " + arr4[0] // 百位

            return parseInt(strnum.substring(1)) == 0 ? b : b + " and " + numToEnglish(parseInt(strnum.substring(1)))
            
        }else{
            let arrnum = parseInt(num).toLocaleString().toString().split(',')
            let len = arrnum.length
            let ret = ""
            console.log('arrnum-->', arrnum)
            
            for(let i = len; i > 0; i--){ // toLocaleString 整数部分每三位加一个逗号,切割成数组后,进行循环
                let index = len - i  //  arr4 的下标
                let res = ''// let num = 1008663
                // arrnum = ['1', '008', '663']
                // length = 3
                // i = 3    index = 0   arrnum[index] = 1
                // i = 2    index = 1   arrnum[index] = 008
                // i = 1    index = 2   arrnum[index] = 663

                if(i > 1){ //
                    res =numToEnglish(parseInt(arrnum[index])) + ' ' + arr4[i -1]
                    ret += res + ' '
                }else{  // 数组的最后一个
                    if(arrnum[index] > 100){
                        ret += numToEnglish(parseInt(arrnum[index]))
                    }else{
                        ret += ' and ' + numToEnglish(parseInt(arrnum[index]))
                    }
                }
            }
            return ret
        }
    }
</script>
</html>

 

  

上一篇:Go数组反转练习


下一篇:虹软arcface人脸识别01