EkBass
Silver Coder
Hi.
I was looking old snippets on one Finnish programming site and noticed this: Ohjelmointiputka: Koodit: QB: Luku kirjoitettuna
It is a QB code that converts numbers to words, in Finnish.
You can guess rest and here is conversion of it in nodejs. I also translated numbers in english. I am almost sure it works just fine now. Though there is way too many numbers to test, so i promise it is almost almost fully functional.
I was looking old snippets on one Finnish programming site and noticed this: Ohjelmointiputka: Koodit: QB: Luku kirjoitettuna
It is a QB code that converts numbers to words, in Finnish.
You can guess rest and here is conversion of it in nodejs. I also translated numbers in english. I am almost sure it works just fine now. Though there is way too many numbers to test, so i promise it is almost almost fully functional.
JavaScript:
/*
Numbers to words snippet for nodejs.
Original idea: https://www.ohjelmointiputka.net/koodivinkit/23425-qb-luku-kirjoitettuna
[email protected] 2024
Public domain, free as air, do-what-you-want license and no promise it wont explod your computer.
*/
// Requires: npm install readline-sync
const readline = require('readline-sync');
// Array to store numbers as words
const numbers = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety",
"hundred", "thousand", "million", "billion", "trillion"
];
function sky(characters) {
let result = "";
// tens and "units"
if (characters[1] === '0') {
if (characters[2] !== '0') {
result = numbers[parseInt(characters[2])];
}
} else if (characters[1] === '1') {
result = numbers[10 + parseInt(characters[2])]; // 11 to 19
} else {
result = numbers[18 + parseInt(characters[1])]; // 20, 30...90
if (characters[2] !== '0') {
result += "-" + numbers[parseInt(characters[2])];
}
}
// Hundreds
if (characters[0] !== '0') {
result = numbers[parseInt(characters[0])] + " " + numbers[28] + (result ? " and " + result : "");
}
return result;
}
// prime function for convert numbers to words
function numberToWords(numberStr) {
if (numberStr.length > 18) {
return "Too big a number!";
}
// "pad" the number string to make its length a multiple of 3
while (numberStr.length % 3 !== 0) {
numberStr = "0" + numberStr;
}
let result = "";
const groupSuffixes = ["", "thousand", "million", "billion", "trillion"];
for (let i = 0; i < numberStr.length / 3; i++) {
const segment = numberStr.substring(numberStr.length - (i + 1) * 3, numberStr.length - i * 3);
const segmentWords = sky(segment);
if (segmentWords) {
if (i > 0) {
result = segmentWords + " " + groupSuffixes[i] + (result ? " " + result : "");
} else {
result = segmentWords + (result ? " " + result : "");
}
}
}
return result || numbers[0];
}
// Lets use it.
const numberInput = readline.question("Enter number: ");
const numberOutput = numberToWords(numberInput);
console.log(numberOutput);