代码之家  ›  专栏  ›  技术社区  ›  Shiva

我使用的javascript代码可以将输入的任何数字转换为等效文本

  •  -1
  • Shiva  · 技术社区  · 15 年前

    例如,4转换为“4”,33333转换为“三万三千三百三十三”。我想用jquery代替普通的javascript。

    以下是完整的代码:

    <script language="javascript" type="text/javascript"> 
    
        function NumberToTextConverter()
        {
            this.TEN = 10;
            this.HUNDRED = 100;
            this.THOUSAND = 1000;
            this.MILLION = 1000000;
            this.BILLION = 1000000000;
            this.wordList = new Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "TEN", "ELEVEN", "Twelve", "Thirteen", "Fourteen", "fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen");
            this.wordList2 = [];
            this.initializeTwentys(); // this would populate the twentys
    
        }
    
        NumberToTextConverter.Convert = function(number)
        {
            var currentConverter = new NumberToTextConverter();
            return currentConverter.Convert(number);
        };
    
        NumberToTextConverter.prototype.Convert = function(number)
        {
            var quotient = Math.floor(number / this.BILLION);
            var remainder = number % this.BILLION;
            var word = "";
            var realValue = "";
            var converter = this;
            if (number < this.BILLION)
            {
                return converter.ConvertToMillions(number);
            }
            else
            {
                var quotientValue = quotient.toString();
                if (quotientValue.length == 3)
                {
                    realValue = realValue + converter.ConvertHundreds(quotient) + " billions ";
                }
                else if (quotientValue.length == 2)
                {
                    realValue = realValue + converter.ConvertToDoubleDigit(quotient) + " billions ";
                }
                else
                {
                    realValue = realValue + this.wordList[quotient] + " billions ";
                }
                realValue = realValue + converter.ConvertToMillions(remainder);
            }
            return realValue;
        };
        NumberToTextConverter.prototype.ConvertToMillions = function(number)
        {
            var quotient = Math.floor(number / this.MILLION);
            var remainder = number % this.MILLION;
            var word = "";
            var realValue = "";
            var converter = this;
            if (number < this.MILLION)
            {
                return converter.ConverToThousands(number);
            }
            else
            {
                var quotientValue = quotient.toString();
                if (quotientValue.length == 3)
                {
                    realValue = realValue + converter.ConvertHundreds(quotient) + " millions ";
                }
                else if (quotientValue.length == 2)
                {
                    realValue = realValue + converter.ConvertToDoubleDigit(quotient) + " millions ";
                }
                else
                {
                    realValue = realValue + this.wordList[quotient] + " millions ";
                }
                realValue = realValue + converter.ConverToThousands(remainder);
            }
            return realValue;
        };
        NumberToTextConverter.prototype.ConverToThousands = function(number)
        {
            var quotient = Math.floor(number / this.THOUSAND);
            var remainder = number % this.THOUSAND;
            var word = "";
            var realValue = "";
            var converter = this;
            if (number < this.THOUSAND)
            {
                return converter.ConvertHundreds(number);
            }
            else
            {
                var quotientValue = quotient.toString();
                if (quotientValue.length == 3)
                {
                    realValue = realValue + converter.ConvertHundreds(quotient) + " thousands ";
                }
                else if (quotientValue.length == 2)
                {
                    realValue = realValue + converter.ConvertToDoubleDigit(quotient) + " thousands ";
                }
                else
                {
                    realValue = realValue + this.wordList[quotient] + " thousands ";
                }
                realValue = realValue + converter.ConvertHundreds(remainder);
            }
            return realValue;
        };
    
        NumberToTextConverter.prototype.ConvertHundreds = function(number)
        {
            var quotient = Math.floor(number / this.HUNDRED);
            var remainder = number % this.HUNDRED;
            var word = "";
            var converter = this;
            if (number >= 100)
            {
                return this.wordList[quotient] + " hundred " + converter.ConvertToDoubleDigit(remainder);
            }
            else
            {
             return   converter.ConvertToDoubleDigit(remainder);
            }
        };
        NumberToTextConverter.prototype.initializeTwentys = function()
        {
            this.wordList2[0] = "";
            this.wordList2[1] = "TEN";
            this.wordList2[2] = "TWENTY";
            this.wordList2[3] = "THIRTY";
            this.wordList2[4] = "FOURTY";
            this.wordList2[5] = "FIFTY";
            this.wordList2[6] = "Sixty";
            this.wordList2[7] = "Seventy";
            this.wordList2[8] = "Eighty";
            this.wordList2[9] = "Ninety";
        };
        NumberToTextConverter.prototype.ConvertSingleDigit = function(number)
        {
            return this.wordList[number];
        };
        NumberToTextConverter.prototype.ConvertToDoubleDigit = function(number)
        {
            var quotient = Math.floor(number / this.TEN);
            var remainder = number % this.TEN;
            var word = "";
            if (number > 19)
            {
                switch (quotient)
                {
                    case 2: word = this.wordList2[2]; break;
                    case 3: word = this.wordList2[3]; break;
                    case 4: word = this.wordList2[4]; break;
                    case 5: word = this.wordList2[5]; break;
                    case 6: word = this.wordList2[6]; break;
                    case 7: word = this.wordList2[7]; break;
                    case 8: word = this.wordList2[8]; break;
                    case 9: word = this.wordList2[9]; break;
                }
                return word + " " + this.wordList[remainder];
            }
            else
            {
                return this.wordList[number];
            }
        };
    
        function PleaseConvert()
        {
    
            var value = document.getElementById("txtNumberInput").value;
            var checkValue = NumberToTextConverter.Convert(parseInt(value));
    
            var currentSpanTag = document.getElementById("spanText");
            currentSpanTag.style.backgroundColor = '#aadd88';
            currentSpanTag.style.border = 'dotted 1px #333377';
            currentSpanTag.innerHTML = checkValue;
        }
    

    感谢您的意见和想法!!我的问题是,用jquery实现这个逻辑是否是个好主意?这是工作代码: http://www.coolaspdotnetcode.com/Web/JavaScriptInfoAndCode.aspx

    4 回复  |  直到 15 年前
        1
  •  9
  •   Paolo Bergantino    15 年前

    “转换为jquery代码”究竟是什么意思?jquery实际上与您发布的代码几乎没有任何关系。它不是一种不同的语言,它没有使JavaScript与众不同的魔力。jquery是一个库,旨在使操作DOM元素和跨浏览器执行常见的javascript任务变得容易。它 Javascript,而且没有一个地方真正适合拥有这样的功能。

    如果你真正的意思是“用这个做一个插件”,那么它是一个5行程序:

    $.fn.humanizeNumber = function() {
        return this.each(function() {
            $(this).html(CALLTHEFUNCTION($(this).html()));
        }
    });
    

    在哪里? CALLTHEFUNCTION 你上面发布的代码的主要功能是什么(我真的不想通过它找到它是什么)。这个插件会让你这样做:

    $('#myelement').humanizeNumber();
    

    转换中的值 #myelement 从“123”到函数返回的任何值。

        2
  •  3
  •   Daniel Moura    15 年前

    如果它已经这样做了,并且工作正常,我不明白为什么要花时间在jquery中实现它。

    除非你想学习/实践jquery。

        3
  •  2
  •   karim79    15 年前

    如果代码有效,那么 ,不要重构。为什么?因为我们在这里的时间不多,在地球上。

        4
  •  1
  •   Tom Ritter    15 年前

    我想最好的地方是 http://refactormycode.com/

    StackOverflow更倾向于直接的问题和答案,而较少用于讨论。