Program to convert a given number to words | Set 2 | Strings at index 0 is not used , it is to make array indexing simple ; Strings at index 0 and 1 are not used , they is to make array indexing simple ; n is 1 - or 2 - digit number ; if n is more than 19 , divide it ; if n is non - zero ; Function to prvar a given number in words ; stores word representation of given number n ; handles digits at ten millions and hundred millions places ( if any ) ; handles digits at hundred thousands and one millions places ( if any ) ; handles digits at thousands and tens thousands places ( if any ) ; handles digit at hundreds places ( if any ) ; handles digits at ones and tens places ( if any ) ; var handles upto 9 digit no change to unsigned var var var to handle more digit number ; convert given number in words
var one = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; var ten = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; function numToWords ( n , s ) { var str = " " ; if ( n > 19 ) { str += ten [ parseInt ( n / 10 ) ] + one [ n % 10 ] ; } else { str += one [ n ] ; } if ( n != 0 ) { str += s ; } return str ; } function convertToWords ( n ) { var out = " " ; out += numToWords ( parseInt ( n / 10000000 ) , " " ) ; out += numToWords ( parseInt ( ( n / 100000 ) % 100 ) , " " ) ; out += numToWords ( parseInt ( ( n / 1000 ) % 100 ) , " " ) ; out += numToWords ( parseInt ( ( n / 100 ) % 10 ) , " " ) ; if ( n > 100 && n % 100 > 0 ) { out += " " ; } out += numToWords ( parseInt ( n % 100 ) , " " ) ; return out ; } var n = 438237764 ; document . write ( convertToWords ( n ) ) ;