0

What I have is the following:

var numbers = new Array("one", "two", "three", ...);

Using this I can get the text version of any number I'd like (by using numbers[index + 1]). The thing is, what if I wanted 23,456,789 converted to text, or even 22? I'd have to spend the rest of my life adding to this array to account for any numbers added.

Is there a standard JS or JQuery function available that'll give me the text-representation of any number? Ideally I'd like them with no spaces and no symbols (i.e. 22=twentytwo, 120=onehundredtwenty).

The application is for this fiddle example. Detailed explanation of said fiddle can be found at this other thread of mine.

Currently, this issue is the biggest hurdle for truly having a use-anywhere code.

I could use something like:

function numbers(a){
    var ones = new Array("one", "two", "three", ...);
    var tens = new Array("", "twenty", "thirty", ...);
}

And then split apart the number and apply the appropriate text. But is there a simpler, existing method? Before I go through with writing the function.

EDIT:

I was hoping for a simple one-liner. In the end all I need are unique classes, based off of their numerical position in a list, so I've got something like...

function toNumber(a) {
    var numbers = new Array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine");

    var sNumber = a.toString();

    for (var i = 0, len = sNumber.length; i < len; i += 1) {
        $('p').append(numbers[sNumber[i]]);
    }
}

Does it well enough.

Community
  • 1
  • 1
Birrel
  • 4,754
  • 6
  • 38
  • 74

1 Answers1

0
var th = ['','thousand','million', 'billion','trillion'];
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,' ');
    }
davethecoder
  • 3,856
  • 4
  • 35
  • 66
  • from source http://ravindersinghdang.blogspot.com/2013/04/convert-numbers-into-words-using.html this was first result in google for your question, discover the power of a search engine :-) – davethecoder Mar 02 '14 at 00:29