Convert digits into words with JavaScript

后端 未结 27 1462
我寻月下人不归
我寻月下人不归 2020-11-22 15:08

I am making a code which converts the given amount into words, heres is what I have got after googling. But I think its a little lengthy code to achieve a simple task. Two R

27条回答
  •  一个人的身影
    2020-11-22 15:50

    I have created customized function ntsConvert() to convert number to words

    function ntsConvert(value) {
    let input = String(value).split('');
    let mapData = {
    	"0": "Zero",
    	"1": "One",
    	"2": "Two",
    	"3": "Three",
    	"4": "Four",
    	"5": "Five",
    	"6": "Six",
    	"7": "Seven",
    	"8": "Eight",
    	"9": "Nine"
    };
    let output = '';
    var tempArray = []
    for (let i = 0; i < input.length; i++) {
    	tempArray.push(mapData[input[i]])
    }
    output = tempArray.join(' ');
    return output;
    }
    
    console.log(ntsConvert(12345)) // 'One Two Three Four Five'

提交回复
热议问题