Wednesday, 4 April 2012

Numbers to Words


Lots of programming involves calculations with numbers. The problem comes when you want to display the result of your calculations and just printing the plain number isn't going to be good enough for what you want. You can of course format the number by adding commas and so forth but sometimes a number as a number isn't appropriate and you need the equivalent of the number in words. Converting a number into words isn't exactly the most straightforward of tasks but it can be done using javascript as the following number to word conversion form demonstrates.
I
f you want to be able to do these conversions on your site then the you will need a Javascript that can do the conversion for you. The simplest way to get one is to take a copy of mine. The first step to obtain it is to select the code from the text box below (there is a highlight all button beneath it to make this easier) 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; i<y; i++) str += dg[n[i]] +' ';} return str.replace(/\s+/g,' ');}
===========================================================
Next you link the script into the head of your page using the following code:
<script type="text/javascript" src="toword.js">
</script>
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.
var words = toWords(num);
Note that this function can convert numbers as big as 999,999,999,999,999 with as many decimal places as you like into words. 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 all acceptable content for the number but it will return "not a number" if it contains anything else. If you want to convert negativ numbers of currency values to words you should remove those symbols from the number first and convert those to words separately.

(or)

with C# Code:


 private string NumberToText(decimal iAmt)
    {
       decimal  v = iAmt;
        if (v == 0)
        {
            return "zero";
        }
        else
        {
            var units = "|One|Two|Three|Four|Five|Six|Seven|Eight|Nine".Split('|');
            var teens = "|eleven|twelve|thir#|four#|fif#|six#|seven#|eigh#|nine#".Replace("#", "teen").Split('|');
            var tens = "|Ten|Twenty|Thirty|Forty|Fifty|Sixty|Seventy|Eighty|Ninety".Split('|');
            var thou = "|Thousand|m#|b#|tr#|quadr#|quint#|sex#|sept#|oct#".Replace("#", "illion").Split('|');
            //var g = (v < style="color: rgb(163, 21, 21);">"minus " : "");
            var g = (v < 0 ? "Minus" : "");
            var w = "";
            var p = 0;
            v = Math.Abs(v);
            while (v > 0)
            {
                int b = (int)(v % 1000);
                if (b > 0)
                {
                    var h = (b / 100);
                    var t = (b - h * 100) / 10;
                    var u = (b - h * 100 - t * 10);
                    var s = ((h > 0) ? units[h] + " Hundred" + ((t > 0 | u > 0) ? " and " : "") : "") + ((t > 0) ? (t == 1 && u > 0) ? teens[u] : tens[t] + ((u > 0) ? "-" : "") : "") + ((t != 1) ? units[u] : ""); s = (((v > 1000) && (h == 0) && (p == 0)) ? " and " : (v > 1000) ? ", " : "") + s; w = s + " " + thou[p] + w;
                }
                v = v / 1000; p++;
            }
            return g + w;
        }
    }


under Button Click Event:

        string strAmount = NumberToText(iAmt);
        Label1.Text = strAmount;



No comments:

Post a Comment