How to Convert Numbers Into Words Using JavaScript

This script gives you flexibility in presenting numbers

Close-Up Of Javascript On Computer Monitor

Degui Adil/EyeEm/Getty Images

Lots of programming involves calculations with numbers, and you can easily format numbers for display by adding commas, decimals, negative signs, and other appropriate characters depending on the kind of number it is.

But you're not always presenting your results as part of a mathematical equation. The Web for the general user is more about words than it is about numbers, so sometimes a number displayed as a number isn't appropriate.

In this case, you need the equivalent of the number in words, not in numerals. This is where you can run into difficulties. How do you convert the numeric results of your calculations when you need the number displayed in words?

Converting a number into words isn't exactly the most straightforward of tasks, but it can be done using JavaScript that isn't too complex.

JavaScript to Convert Numbers Into Words

If you want to be able to do these conversions on your site, you will need a JavaScript code that can do the conversion for you. The simplest way to do this is to use the code below; just select the code and copy it into a file called toword.js.

// Convert numbers to words
// copyright 25th July 2006, by Stephen Chapman http://javascript.about.com
// permission to use this Javascript on your web page is granted
// provided that all of the code (including this copyright notice) is
// used exactly as shown (you can change the numbering system if you wish)

// American Numbering System
var th = ['','thousand','million', 'billion','trillion'];
// uncomment this line for English Number System
// var th = ['','thousand','million', 'milliard','billion'];

var dg = ['zero','one','two','three','four',
'five','six','seven','eight','nine']; var tn =
['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen',
'seventeen','eighteen','nineteen']; var tw = ['twenty','thirty','forty','fifty',
'sixty','seventy','eighty','ninety']; function toWords(s){s = s.toString(); s =
s.replace(/[\, ]/g,''); if (s != parseFloat(s)) return 'not a number'; var x =
s.indexOf('.'); if (x == -1) x = s.length; if (x > 15) return 'too big'; var n =
s.split(''); var str = ''; var sk = 0; for (var i=0; i < x; i++) {if
((x-i)%3==2) {if (n[i] == '1') {str += tn[Number(n[i+1])] + ' '; i++; sk=1;}
else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}} else if (n[i]!=0) {str +=
dg[n[i]] +' '; if ((x-i)%3==0) str += 'hundred ';sk=1;} if ((x-i)%3==1) {if (sk)
str += th[(x-i-1)/3] + ' ';sk=0;}} if (x != s.length) {var y = s.length; str +=
'point '; for (var i=x+1; istr.replace(/\s+/g,' ');}

Next, link the script into the head of your page using the following code:

var words = toWords(num);

The final step is to call the script to perform the conversion to words for you. 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.

Numbers to Words Limitations

Note that this function can convert numbers as big as 999,999,999,999,999 into words and with as many decimal places as you like. If you try to convert a number bigger than that it will return "too big."

Numbers, commas, spaces, and a single period for the decimal point are the only acceptable characters that can be used for the number being converted. If it contains anything beyond these characters, it will return "not a number."

Negative Numbers

If you want to convert negative numbers of currency values to words you should remove those symbols from the number first and convert those to words separately.

Format
mla apa chicago
Your Citation
Chapman, Stephen. "How to Convert Numbers Into Words Using JavaScript." ThoughtCo, Apr. 5, 2023, thoughtco.com/how-to-convert-numbers-to-words-with-javascript-4072535. Chapman, Stephen. (2023, April 5). How to Convert Numbers Into Words Using JavaScript. Retrieved from https://www.thoughtco.com/how-to-convert-numbers-to-words-with-javascript-4072535 Chapman, Stephen. "How to Convert Numbers Into Words Using JavaScript." ThoughtCo. https://www.thoughtco.com/how-to-convert-numbers-to-words-with-javascript-4072535 (accessed March 19, 2024).