admin管理员组文章数量:1356743
For example, 4 is converted to "Four" and 33333 is converted to "Thirty three thousands three hundred and thirty three". I am thinking of using JQUERY instead of plain JAVASCRIPT.
Here is the code in its entirety:
<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;
}
Your opinions and ideas are appreciated!! My Question is whether it would be good idea to spend time by implementing this logic using JQUERY? Here is the working code : .aspx
For example, 4 is converted to "Four" and 33333 is converted to "Thirty three thousands three hundred and thirty three". I am thinking of using JQUERY instead of plain JAVASCRIPT.
Here is the code in its entirety:
<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;
}
Your opinions and ideas are appreciated!! My Question is whether it would be good idea to spend time by implementing this logic using JQUERY? Here is the working code : http://www.coolaspdotnetcode./Web/JavaScriptInfoAndCode.aspx
Share Improve this question edited Jun 10, 2009 at 18:47 Shiva asked Jun 9, 2009 at 21:15 ShivaShiva 1,4092 gold badges16 silver badges32 bronze badges 8- 3 And your question is...? – Dan Lew Commented Jun 9, 2009 at 21:15
- So, you want SO to refactor this as jQuery script. How thoughtful ;P – karim79 Commented Jun 9, 2009 at 21:17
- 1 From the FAQ stackoverflow./faq : "It's also perfectly fine to ask and answer your own programming question, but pretend you're on Jeopardy: phrase it in the form of a question." – artlung Commented Jun 9, 2009 at 21:17
- @Daniel - his question is implicit. Make the above into jQuery. Any takers? – karim79 Commented Jun 9, 2009 at 21:18
- @Daniel- I assume he wants us to give him the jquery equivalent – TStamper Commented Jun 9, 2009 at 21:18
4 Answers
Reset to default 9What exactly do you mean by "convert to jQuery code"? jQuery has really pretty much nothing to do with the code you posted. It is not a different language, it has no magic that will make the Javascript any different. jQuery is a library intended to make it easy to manipulate DOM elements and perform mon Javascript tasks cross-browser. It is Javascript, and there's nowhere really where it would fit to have a function such as this one.
If what you really mean is "make a plugin out of this", then it's a 5 liner:
$.fn.humanizeNumber = function() {
return this.each(function() {
$(this).html(CALLTHEFUNCTION($(this).html()));
}
});
Where CALLTHEFUNCTION
is whatever is the main function of the code you posted above (I don't really care to go through it and find what it is). That plugin would then let you do this:
$('#myelement').humanizeNumber();
To convert the value in #myelement
from "123" to whatever your function returns.
If it's already done this way and it's working I don't see why spending time implementing this in jQuery.
Unless if you want to learn/practice jQuery.
If your code works, then no, don't refactor. Why? Because we don't have much time here, on Earth.
I think a better place for this would be http://refactormycode./
Stackoverflow is more geared towards direct Question and Answers, and less for discussions.
本文标签:
版权声明:本文标题:jquery - I have working Javascript code that that converts any number entered into its text equivalent - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744069593a2585643.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论