Tuesday, May 12, 2020

Converting Numbers Into Words Using JavaScript

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 youre 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 isnt 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 isnt exactly the most straightforward of tasks, but it can be done using JavaScript that isnt 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 Systemvar 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 i0; i x; i) {if((x-i)%32) {if (n[i] 1) {str tn[Number(n[i1])] ; i; sk1;}else if (n[i]!0) {str tw[n[i]-2] ;sk1;}} else if (n[i]!0) {str dg[n[i]] ; if ((x-i)%30) str hundred ;sk1;} if ((x-i)%31) {if (sk)str th[(x-i-1)/3] ;sk0;}} if (x ! s.length) {var y s.length; str point ; for (var ix1; 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.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.