Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Number to words QB -> nodejs conversion.

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.

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);
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom