JavaScript numbers to Words

前端 未结 24 1683
死守一世寂寞
死守一世寂寞 2020-11-22 14:39

I\'m trying to convert numbers into english words, for example 1234 would become: \"one thousand two hundred thirty four\".

My Tact

24条回答
  •  鱼传尺愫
    2020-11-22 15:08

    I tried Muhammad's solution, but had some issues and wanted to use decimals so I made some changes and converted to coffeescript and angular. Please bear in mind that js and coffeescript are not my strong suits, so use with care.

    $scope.convert = (number, upper=0) ->
    number = +number
    # console.log "inside convert and the number is:  " + number
    
    if number < 0
        # console.log 'Number Must be greater than zero = ' + number
        return ''
    
    if number > 100000000000000000000
        # console.log 'Number is out of range = ' + number
        return ''
    if isNaN(number)
        console.log("NOT A NUMBER")
        alert("Not a number = ")
        return ''
    else
        console.log "at line 88 number is:  " + number
        quintillion = Math.floor(number / 1000000000000000000)
        ### quintillion ###
    
        number -= quintillion * 1000000000000000000
        quar = Math.floor(number / 1000000000000000)
        # console.log "at line 94 number is:  " + number
    
        ### quadrillion ###
    
        number -= quar * 1000000000000000
        trin = Math.floor(number / 1000000000000)
        # console.log "at line 100 number is:  " + number
    
        ### trillion ###
        number -= trin * 1000000000000
        Gn = Math.floor(number / 1000000000)
        # console.log "at line 105 number is:  " + number
    
        ### billion ###
    
        number -= Gn * 1000000000
        million = Math.floor(number / 1000000)
        # console.log "at line 111 number is:  " + number
    
        ### million ###
    
        number -= million * 1000000
        Hn = Math.floor(number / 1000)
        # console.log "at line 117 number is:  " + number
    
        ### thousand ###
    
        number -= Hn * 1000
        Dn = Math.floor(number / 100)
        # console.log "at line 123 number is:  " + number
    
        ### Tens (deca) ###
    
        number = number % 100
        # console.log "at line 128 number is:  " + number
    
        ### Ones ###
        tn      = Math.floor(number / 10)
        one     = Math.floor(number % 10)
        # tn = Math.floor(number / 1)
        change  = Math.round((number % 1) * 100)
        res     = ''
        # console.log "before ifs"
        if quintillion > 0
            res += $scope.convert(quintillion) + ' Quintillion'
        if quar > 0
            res += $scope.convert(quar) + ' Quadrillion'
        if trin > 0
            res += $scope.convert(trin) + ' Trillion'
        if Gn > 0
            res += $scope.convert(Gn) + ' Billion'
        if million > 0
            res += (if res == '' then '' else ' ') + $scope.convert(million) + ' Million'
        if Hn > 0
            res += (if res == '' then '' else ' ') + $scope.convert(Hn) + ' Thousand'
        if Dn
            res += (if res == '' then '' else ' ') + $scope.convert(Dn) + ' Hundred'
        # console.log "the result is:  " + res
        ones = Array('', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eightteen', 'Nineteen')
        tens = Array('', '', 'Twenty', 'Thirty', 'Fourty', 'Fifty', 'Sixty', 'Seventy', 'Eigthy', 'Ninety')
        # console.log "the result at 161 is:  " + res
        if tn > 0 or one > 0
            if !(res == '')
                # res += ' and '
                res += ' '
                # console.log "the result at 164 is:  " + res
            if tn < 2
                res += ones[tn * 10 + one]
                # console.log "the result at 168is:  " + res
            else
                res += tens[tn]
                if one > 0
                    res += '-' + ones[one]
                # console.log "the result at 173 is:  " + res
        if change > 0
            if res == ''
                res =  change + "/100"
            else
                res += ' and ' + change + "/100"
    
        if res == ''
            console.log 'Empty = ' + number
            res = ''
        if +upper == 1
            res = res.toUpperCase()
        $scope.newCheck.amountInWords = res
        return res
    

    $scope.is_numeric = (mixed_var) -> # console.log "mixed var is: " + mixed_var (typeof mixed_var == 'number' or typeof mixed_var == 'string') and mixed_var != '' and !isNaN(mixed_var)

提交回复
热议问题