Results 1 to 2 of 2

Thread: Converting numbers to words (english)

  1. #1
    Sencha - Services Team Stju's Avatar
    Join Date
    Dec 2008
    Location
    Redwood city, California
    Posts
    292
    Vote Rating
    3
      0  

    Default Converting numbers to words (english)

    Code:
    Ext.define('Ext.util.NumbersToWords',{
        singleton:true,
    
        ones:[
            '',
            'one',
            'two',
            'three',
            'four',
            'five',
            'six',
            'seven',
            'eight',
            'nine',
            'ten',
            'eleven',
            'twelve',
            'thirteen',
            'fourteen',
            'fiveteen',
            'sixteen',
            'seventeen',
            'eighteen',
            'nineteen'
        ],
    
        tens:[
            '',
            'twenty',
            'thirty',
            'fourty',
            'fifty',
            'sixty',
            'seventy',
            'eighty',
            'ninety'
        ],
    
        triplets:[
            '',
            'thousand',
            'millon',
            'billion',
            'trillion'
        ],
    
        convert: function(number) {
            var n = (+number);
    
            if (n < 0)
                return 'minus ' + this.applyConversion(n * (-1), 0);
    
            if (n == 0)
                return 'zero';
    
            return this.applyConversion(n, 0);
        },
    
        applyConversion: function(num, tri) {
            var r,
                x,
                y,
                str = '',
                gap = ' ';
    
            r = Math.floor(num / 1000);
            x = Math.floor(num / 100) % 10;
            y = num % 100;
    
            // do hundreds
            if (x == 1){
                str = this.ones[x] + gap + 'hundred';
            }
            else if (x > 0){
                str = this.ones[x] + gap + 'hundreds';
            }
    
            // do ones and tens
            if (y < 20){
                str = str + gap + this.ones[y];
            } else{
                str = str + gap + this.tens[parseInt(y / 10) -1] + gap + this.ones[y % 10];
            }
    
            // add triplet modifier only if there
            // is some output to be modified...
            if (str != ''){
                str = str + gap +this.triplets[tri];
            }
    
            // continue recursing?
            if (r > 0)
                return this.applyConversion(r, tri + 1) + gap + str.trim();
            else
                return str.trim();
        }
    });

    Sample usage:
    Ext.util.NumbersToWords.convert(121999)
    will result in
    "one hundred twenty one thousand nine hundreds ninety nine"


  2. #2
    Sencha User jerome76's Avatar
    Join Date
    Apr 2012
    Location
    New Jersey
    Posts
    1,149
    Vote Rating
    101
      0  

    Default

    Cool. Do you think there should more more punctuation or conjunctions such as your example?
    Code:
    Ext.util.NumbersToWords.convert(121999);
    results in
    one hundred twenty-one thousand nine hundred ninety-nine

    as seen here: http://www.easysurf.cc/cnvert18.htm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •