Integer to english words javascript

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

/*
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 2^31 - 1.
For example,
123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
less than 2 billion ->
*/
const upToTwenty = ['', "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten","Eleven",
"Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"];
const tens = ['', "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
const thousands = ['', 'Thousand', 'Million', 'Billion'];
function numToWords(num) {
// convert the input number to a string
// if the number is bigger than 12 places
// return 0 it would be too big
if (num.toString().length > 12) return 'TOO BIG';
if (num === 0) {
return 0;
}
let counter = 0;
// crete a results string ''
let res = '';
// We have strings representations
// while the number is > 0
while (num > 0) {
// the number is % 1000 not 0
// set out results sting equal to helper(current Num%1000) + counter a res
if (num % 1000 !== 0) {
res = `${getNums(num%1000)} ${thousands[counter]} ${res}`;
}
// num = num/1000
// counter++
num = Math.floor(num / 1000);
counter++;
}
return res;
}
const getNums = number => {
// if the number is 0
// return '';
if (number === 0) {
return '';
}
else if (number < 20) {
return upToTwenty[number];
}
else if (number < 100) {
return `${tens[Math.floor(number/10)]} ${getNums(number % 10)}`;
}
else {
return `${upToTwenty[Math.floor(number/100)]} Hundred ${getNums(number%100)}`;
}
// if the number is less than 20
// return upToTwenty[number]
// else if the number is < 100
// return floor this number tens[number/10] + getNums(number % 10)
// This case should handle hundreds
// return floor upToTwenty[num/100] + hundreds getnums(number%100)
}
let testNum = 1234 *5678 * 9000
let result = numToWords(testNum);
console.log(result);

How do you write numbers in words in JavaScript?

var words = toWords(num); To get a number converted to words you just need to call the function passing it the number you want to convert and the corresponding words will be returned.

How can I convert a number to words?

Type the formula =SpellNumber(A1) into the cell where you want to display a written number, where A1 is the cell containing the number you want to convert. You can also manually type the value like =SpellNumber(22.50).

How are numbers converted to string in JavaScript?

JavaScript Number to String – How to Use toString to Convert an Int into a String.
var num = 24; var str = num. toString(); console. ... .
toString(); // Error: Invalid or unexpected token (24). toString(); // "24" (9.7). ... .
var str = (5). toString(2); console. ... .
var arr = [ "Nathan", "Jack" ]; var str = arr. toString(); console..

How do you convert digits to words in Java?

Converting Large Numbers into Words.
import java.text.DecimalFormat;.
public class NumberToWordExample2..
//string type array for one digit numbers..
private static final String[] twodigits = {"", " Ten", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety"};.