Convert Number to Words

Is there a no-code formula that can convert a number (3) to text (three)? I need to do it in the front end ideally.

No, but if it’s only to convert 0 to 9, you can create your own pretty easily with a custom formula, even in no code, using the switch function :slight_smile:

2 Likes

As alexis said, it’s a pretty simple fromula, copilot or GPT might help :slight_smile:

1 Like
function test(n) {
    if (n < 0)
      return false;
	 single_digit = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine']
	 double_digit = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen']
	 below_hundred = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']
	if (n === 0) return 'Zero'
	function translate(n) {
		word = ""
		if (n < 10) {
			word = single_digit[n] + ' '
		}
		else if (n < 20) {
			word = double_digit[n - 10] + ' '
		}
		else if (n < 100) {
			rem = translate(n % 10)
			word = below_hundred[(n - n % 10) / 10 - 2] + ' ' + rem
		}
		else if (n < 1000) {
			word = single_digit[Math.trunc(n / 100)] + ' Hundred ' + translate(n % 100)
		}
		else if (n < 1000000) {
			word = translate(parseInt(n / 1000)).trim() + ' Thousand ' + translate(n % 1000)
		}
		else if (n < 1000000000) {
			word = translate(parseInt(n / 1000000)).trim() + ' Million ' + translate(n % 1000000)
		}
		else {
			word = translate(parseInt(n / 1000000000)).trim() + ' Billion ' + translate(n % 1000000000)
		}
		return word
	}
	 result = translate(n) 
	return result.trim()+'.'
}

n = 1002
console.log("Number n = " +n)
console.log("In word: "+test(n));
n = 1279
console.log("Number n = " +n)
console.log("In word: "+test(n));
n = 127900
console.log("Number n = " +n)
console.log("In word: "+test(n));
n = 1279000
console.log("Number n = " +n)
console.log("In word: "+test(n));

You could try something like the above

3 Likes

I looooove the switch formula!

Here’s a short video tutorial on how it works.

2 Likes

Thanks everyone. The switch() formula will be useful in other scenarios, but I think will be a bit too cumbersome here. Thanks @Joyce and @Alexis.

I tried using co-pilot but it rarely works anymore (see this thread, bug logged).

I tried using ChatGPT, but I can’t figure out how to properly reference the variable value that I want to convert from a number to text. ChatGPT gave me the below, but as it is WeWeb is returning ‘undefined’. What do I need ot change for this to run in WeWeb? Thanks.

const numberToWords = (num) => {
    const ones = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
    const teens = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
    const tens = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
    
    if (num === 0) {
        return 'zero';
    }
    
    if (num < 10) {
        return ones[num - 1];
    }
    
    if (num >= 11 && num <= 19) {
        return teens[num - 11];
    }
    
    if (num % 10 === 0 && num < 100) {
        return tens[Math.floor(num / 10) - 1];
    }
    
    if (num >= 20 && num < 100) {
        return `${tens[Math.floor(num / 10) - 1]} ${ones[num % 10 - 1]}`;
    }
    
    return 'Number out of range';
};

const inputNumber = 23; // Replace with your desired number
console.log(`${inputNumber} in words is: ${numberToWords(inputNumber)}`);

Add this as the final line.

return numberToWords(inputNumber)
3 Likes

Worked! Thanks @jaredgibb.