Number To Word Translator
Aug 22, 2002
Don't know if there is one of these online, so I made me own. Simple function that outputs a number as words. First argument is the number, second argument is flag (default = false) that controls whether output is cardinal or ordinal - as in "one" or "first".
Goodness knows if I have the grammer correct, especially when the numbers get big. Enjoy!
LIMITS:
1) Doesn't work with WebTV or below NS 4. Changing the large literal array into a common array should fix that, but I didn't bother.
2) Converts number into an integer before wording number. So, no "one point forty-five" stuff. This function is a good start for anyone who wants to do that though.
3) Always returns, "zero" for 0. Couldn't find cardinal equivalent to "zero"... zeroeth?
/*
Num2Word, ouputs written number in human language format
ARGUMENTS:
-----------
Nbr: num, the number to be worded. This number is converted into an integer.
Crd: bol, flags whether output is cardinal or ordinal number. Cardinal is used when describing the order of items, like "first" or "second" place.
*/
function Num2Word(num,fmt) {
//_ arguments
num = Math.round(num).toString(); // round value
if (num == 0) return 'zero' // if number given is 0, return and exit
//_ locals
// word numbers
var wnums = [['hundred','thousand','million','billion','trillion','zillion'],['one','first','ten','','th'],['two','second','twen',0,0],['three','third','thir',0,0],['four','fourth',0,0,0],['five','fifth','fif',0,0],['six','sixth',0,0,0],['seven','seventh',0,0,0],['eight','eighth','eigh',0,0],['nine','ninth',0,0,0],['ten',],['eleven',],['twelve','twelfth'],['thirteen',],['fourteen',],['fifteen',],['sixteen',],['seventeen',],['eighteen',],['nineteen',]];
// digits outside triplets
var dot = (num.length % 3) ? num.length % 3 : 3;
// number of triplets in number
var sets = Math.ceil(num.length/3);
// result string, word as number
var rslt = ''
//_ convert every three digits
for (var i = 0; i < sets; i++) { // for each set of triplets
// capture set of numbers up to three digits
var subt = num.substring((!i) ? 0 : dot + (i - 1) * 3,(!i) ? dot : dot + i * 3);
if (subt != 0) { // if value of set is not 0...
var hdec = (subt.length > 2) ? subt.charAt(0) : 0; // get hundreds digit
var ddec = subt.substring(Math.max(subt.length - 2,0),subt.length); // get double digits
var odec = subt.charAt(subt.length - 1); // get one's digit
// hundreds digit
if (hdec != 0) rslt += ' ' + wnums[hdec][0] + '-hundred ' + ((fmt && ddec == 0) ? 'th' : '');
// add double digit
if (ddec < 20 && 9 < ddec) { // if less than 20...
// add double digit word
rslt += ' ' + ((fmt) ? ((wnums[ddec][1]) ? wnums[ddec][1] : wnums[ddec][0] + 'th') : wnums[ddec][0]);
} else { // otherwise, add words for each digit...
// add "and" for last set without a tens digits
if ((0 < hdec || 1 < sets) && i + 1 == sets && 0 < ddec && ddec < 10) rslt += 'and '
// tens digit
if (19 < ddec) rslt += wnums[ddec.charAt(0)][(wnums[ddec.charAt(0)][2]) ? 2 : 0] + ((i + 1 == sets && odec == 0 && fmt) ? 'tieth' : 'ty') + ((0 < odec) ? '-' : ' ');
// one's digit
if (0 < odec) rslt += wnums[odec][(i + 1 == sets && fmt) ? 1 : 0];
}
// add place for set
if (i + 1 < sets) rslt += ' ' + wnums[0][sets - i - 1] + ' ' // if not last set, add place
} else if (i + 1 == sets && fmt) { // otherwise, if this set is zero, is the last set of the loop, and the format (fmt) is cardinal
rslt += 'th' // add cardinal "th"
}
}
//_ return result
return rslt
}
Test the result of various valies like so
for (var i = 0; i < 50; i++) {
var x = Math.round(Math.random() * 5000000);
document.write(x,' -> ' + Num2Word(x).bold(),'<BR>');
}
Use this function towards whatever means you deem necessary. Pease credit me, Bemi Faison, if you use this script.
View 3 Replies
Jul 14, 2010
I am currently seeking help on how to best approach finding a solution to this. Essentially I have a textarea which a user is supposed to be typing a text based email message(no HTML) The textarea has a column width of 80 However there is a recommended width of 53 characters per line. Obviously the user can just keep on typing away past the 53 character recommended width if they want, but I want to add either a form button or a link with an onClick thatanalyzes the contents of the textarea. It should insert a hard return when it reaches 53 characters, but if the 53rd character is in the middle of a word it should count back to the previous space and do the line wrap there.
Can anyone point me in the right direction? I assumed there has to a solution using jQuery, but I am at a loss. I did come across some javascript which works, but it doesn't take into account the fact that the 53rd character maybe in the middle of a word.
[Code]...
View 2 Replies
View Related
Jul 14, 2010
I have a textarea with a specific width. I have wrap="off" set because I don't want to force my users to wrap if they don't want to. The reason for this is because this textarea is where the user is typing an email message. However I have a background image set on the textarea with a verticle line going down the textarea which indicates to the user the "Recommend Width"What I want to do is provide them with a link to click on which says something like "Wrap Lines" and when they click on it, the text within the textarea would wrap to the "Recommended Width" line in the background image. The maximum length of a line should be 53 characters when they click on "Wrap Lines" link.So I did some searching around and the code I came up with is:
Code:
function showLines(max, text) {
max--;
text = "" + text;
var temp = "";
var chcount = 0;
[Code].....
View 5 Replies
View Related
Mar 31, 2009
I'm a novice in javascript programming. I need to create a program that automatically sends websites to a free translating site (the part where it asks you to input a url for website translation), and automatically redirect the user to the translated site. I basically just need to know how to send the url to that box, and submit the result, and redirect the user there.
View 2 Replies
View Related
Feb 4, 2011
I have developed my own fake language like pig latin but ever so slightly different, I want to make a translator for it as there are so many for pig latin,english-my language:
1: put the first letter at the end (hello becomes elloh)
2: put an 'a' 2 letters in (elloh becomes elaloh)
obviously if the inputted word had a capital letter at the begining, then the new first letter should be in capitals, and the translator should be able to translate whole sentences not just word by word,I have downloaded countless javascript/html files for pig latin translating because obviously the first step for translating to the 2 languages is the same and ive managed to stop it adding the 'ay' or 'way' to the end of the word as it is in pig latin bu i dont think ive done it very efficiently?
View 1 Replies
View Related
Nov 21, 2010
Imagine the following input:
Code:
<form action="search.php" method="get">
Search: <input type="text" name="search" />
<input type="hidden" name="searchOriginal" value="search" />
[code].....
What I would like to do is: Use google AJAX API to see if the search string (search) is different of english. I would like to translate it and send it like that to search.php. So it would appear already translated like this in the URL: search.php?search=translatedString. And search.php would also see it translated.
View 6 Replies
View Related
Jun 16, 2011
When updating our agency's webpage daily, [URL] I am looking for a shortcut to save time.. When I update air quality numbers. I have to put the conditions. Ex.. 50 = good 100= moderate etc etc. As you see here(bold and underlined)
[Code]...
Well instead of having to type good or moderate every-time to correlate with the numbers, I want to find a way the category can automatically default correctly when I put in just the number. Ex, if I put in the number 100, it automatically knows to issue/ put Moderate with out me having to type it. I semi wrote a code .. Yet can't seem to know how to execute. Seeing if Maybe I can get some assistance.
[Code]...
View 4 Replies
View Related
Nov 29, 2011
I have a function below where every time a question is submitted, it will add a new row in the table with a textbox which allows numbers entry only. My question is that I don't know how to code these features in this function:
I want the text box to be between 0 and 100, so if text box contains a number which is above 100, it will automatically change the number to the maximum number which is 100.
Does any one know how to code this in my function below in javascript:
Code:
View 1 Replies
View Related
Nov 28, 2011
I am trying to figure out how to make a random number I can plug into a script count down from that number at certain times of the day until it reaches 0. I would like it to reset itself at midnight every day. I'm trying to make it work with a script I found on here that resets itself at midnight every day. So instead of it counting down too fast, it would count down to the next number after a randomly generated number of minutes until it reaches 0. But it wouldn't necessarily have to end at 0 at midnight. It could go from 845 to 323 at the end of the day at a slower pace. Is that possible?
View 5 Replies
View Related
Mar 13, 2010
ielementfrompoint () gets an element at a given position (x,y). how can I get a single word (if it exists) from a given position? to sum up i need this: f(x,y)=word ( or null - in case there is no word). x, y is given NOT obtained with some mouseover event.
View 1 Replies
View Related
Jul 20, 2005
I have what seems to
be a robust, working word counter script. I post it here to benefit
others that might want this in the future and so that if I ever lose
my copy I can come back here to find it :) Some other scripts that I
used for inspiration failed when confronted with whitespace before the
string or miscalculated when encountering linefeeds and other
non-space spaces, so I made mine better. Definition of words for this
exercise is contiguous groups of characters separated by whitespace. Code:
View 2 Replies
View Related
Aug 9, 2002
This JavaScript is a "Word Filter". It is a type of form validator. When the user submits some text, the validator will check the text for words that has to be filtered.
The words that have to be filtered must be added to the array swear_words_arr. When the user types the text and hits the submit button, if the text contains any word that is present in the array swear_words_arr, the form will not be submitted.
The script can be used for validation of swear words etc.
<html>
<head>
<title>Word Filter</title>
<!--BEGIN WORD FILTER JAVASCRIPT-->
<script language="JavaScript1.2">
// Word Filter
// (c) 2002 Premshree Pillai
// http://www.qiksearch.com
// http://javascript.qik.cjb.net
var swear_words_arr=new Array("bloody","war","terror");
var swear_alert_arr=new Array;
var swear_alert_count=0;
function reset_alert_count()
{
swear_alert_count=0;
}
function validate_user_text()
{
reset_alert_count();
var compare_text=document.form1.user_text.value;
for(var i=0; i<swear_words_arr.length; i++)
{
for(var j=0; j<(compare_text.length); j++)
{
if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).toLowerCase())
{
swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i].length));
swear_alert_count++;
}
}
}
var alert_text="";
for(var k=1; k<=swear_alert_count; k++)
{
alert_text+="
" + "(" + k + ") " + swear_alert_arr[k-1];
}
if(swear_alert_count>0)
{
alert("The form cannot be submitted.
The following illegal words were found:
_______________________________
" + alert_text + "
_______________________________");
document.form1.user_text.select();
}
else
{
document.form1.submit();
}
}
function select_area()
{
document.form1.user_text.select();
}
window.onload=reset_alert_count;
</script>
<!--BEGIN WORD FILTER JAVASCRIPT-->
</head>
<body bgcolor="#FFFFFF">
<!--BEGIN FORM-->
<table cellpadding="10" style="border:2 solid #FF9900" width="200" align="center"><tr><td>
<form name="form1" method="post" action="post.cgi">
<center><font face="Times New Roman" size="6pt" color="#606060"><b><i>Word Filter</i></b></font></center>
<table><tr><td></td></tr></table>
<textarea rows="3" cols="40" name="user_text" style="border:2 solid #808080; font-family:verdana,arial,helvetica; font-weight:normal; font-size:10pt" onclick="select_area()">Enter your text here...</textarea>
<table><tr><td></td></tr></table>
<center><input type="button" style="background:#EFEFEF; border:2 solid #808080; width:100%; cursor:pointer" value="Submit" onclick="validate_user_text();"></center>
</form>
</td></tr></table>
<!--END FORM-->
</body>
</html>
View 1 Replies
View Related