Skip to content

Instantly share code, notes, and snippets.

@mark05e
Created April 2, 2024 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark05e/beb1633d4e0470420f9ef93de7af6020 to your computer and use it in GitHub Desktop.
Save mark05e/beb1633d4e0470420f9ef93de7af6020 to your computer and use it in GitHub Desktop.
Numbers to Words.js
// Based on https://www.geeksforgeeks.org/program-to-convert-a-given-number-to-words-set-2/
var ones = {
0: "", // Not used, to make array indexing simple
1: "one ",
2: "two ",
3: "three ",
4: "four ",
5: "five ",
6: "six ",
7: "seven ",
8: "eight ",
9: "nine ",
10: "ten ",
11: "eleven ",
12: "twelve ",
13: "thirteen ",
14: "fourteen ",
15: "fifteen ",
16: "sixteen ",
17: "seventeen ",
18: "eighteen ",
19: "nineteen "
};
var tens = {
0: "", // Not used, to make array indexing simple
1: "", // Not used, as we handle special cases in numbersToWords
2: "twenty ",
3: "thirty ",
4: "forty ",
5: "fifty ",
6: "sixty ",
7: "seventy ",
8: "eighty ",
9: "ninety "
};
function numToWords(number, unitSuffix) {
var words = "";
// Handle hundreds digit
if (Math.floor(number / 100) > 0) {
// Add hundreds word
words += ones[Math.floor(number / 100)] + "hundred ";
}
// Handle remaining tens and ones
number %= 100; // Get remaining number after removing hundreds
// Use existing logic for tens and ones
if (number > 19) {
words += tens[parseInt(number / 10)] + ones[number % 10];
} else if (number > 0) {
words += ones[number];
}
// Add the specified string unit only if number is non-zero
if (number > 0) {
words += unitSuffix;
}
return words;
}
// Function to convert the integer part of a number to words
function convertIntegerToWords(n) {
// stores word representation of given number n
var out = "";
// handles digits at billions places (if any)
out += numToWords(Math.floor(n / 1000000000), "billion ");
// handles digits at millions places (if any)
out += numToWords(Math.floor((n / 1000000) % 1000), "million ");
// handles digits at thousands places (if any)
out += numToWords(Math.floor((n / 1000) % 1000), "thousand ");
// handles digits at hundreds places (if any)
out += numToWords(Math.floor((n / 100) % 10), "hundred ");
// adds 'and' if the number is greater than 100 and the remaining part is non-zero
if (n > 100 && n % 100 > 0) {
out += "and ";
}
// handles digits at ones and tens places (if any)
out += numToWords(Math.floor(n % 100), "");
return out;
}
// Function to convert the fractional part of a number to words
function convertFractionToWords(n) {
// Convert the fractional part to words
var fractionalWords = "";
if (n > 0) {
fractionalWords = "point ";
var fractionalString = n.toFixed(2).split('.')[1]; // Convert to fixed length decimal string
fractionalWords += numToWords(Math.floor(fractionalString % 100), ""); // handles digits at ones and tens places (if any)
}
return fractionalWords.trim();
}
// Example Usage
n = 555.77;
console.log(convertIntegerToWords(n));
console.log(convertFractionToWords(n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment