开发者

How to display the numbers inputed in a text field?

I am creating a form with one field to input开发者_运维百科 numbers . How can I show the value of the numbers in text format in the below text field ?

Eg: If I enter the number 2000 in the text field , the number is displayed below in words ("Two Thousand").


Have you tried google? Second hit


here you are

function readable_number(num){
    var bits_a = ["thousand", "million", "billion", "trillion", "zillion"];
    var bits_b = ["ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"];
    var bits_c = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];

    if (num==0){return 'zero';}

    var result = (num<0)?'minus ':'';

    num = Math.abs(num);
    for(var i=bits_a.lenght; i>0; i--){
        var p = pow(1000, i);
        if (num > p){
            var b = Math.floor(num/p);
            num -= p * b;
            result += readable_number(b) + ' ' + bits_a[i-1];
            result += ((num)?', ':'');
        }
    }
    if (num > 100){
        var b = Math.floor(num/100);
        num -= 100 * b;
        result += readable_number(b) + ' hundred' + ((num)?' and ':' ');
    }
    if (num >= 20){
        var b = Math.floor(num/10);
        num -= 10 * b;
        result += bits_b[b-1] + ' ';
    }
    if (num){
        result += bits_c[num-1];
    }
    return result;
}

alert(readable_number(1234));


I guess you could use the "KeyUp" function which on the first textbox and have a case statement to populate the second one. Something like below.

  $(document).ready(function{} {
       $("#txt1").keyup(function() {

            case ()....           
        });
  });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜