Calculation with javascript - javascript

I have a script that completes a calculation for each row of my table and then add the rows together. The calculation is alway correct if it consists of the first row only. For some reason, I'm getting an incorrect calculation when the other rows are included in the calculation.
var total = 0;
var a1 = Number(this.getField("o1i").value);
var a2 = Number(this.getField("o1iother").value);
var b1 = Number(this.getField("o2i").value);
var b2 = Number(this.getField("o2iother").value);
var c1 = Number(this.getField("o3i").value);
var c2 = Number(this.getField("o3iother").value);
var d1 = Number(this.getField("o4i").value);
var d2 = Number(this.getField("o4iother").value);
var e1 = Number(this.getField("o5i").value);
var e2 = Number(this.getField("o5iother").value);
var t = Number(this.getField("time").value);
for(var i=1; i<=15; i++){
if(this.getField("ins."+i).valueAsString == "o1" &&
this.getField("mins"+i).valueAsString != "")
total += t*a1+a2;
else if(this.getField("ins."+i).valueAsString == "o2" &&
this.getField("mins"+i).valueAsString != "")
total += t*b1+b2;
else if(this.getField("ins."+i).valueAsString == "o3" &&
this.getField("mins"+i).valueAsString != "")
total += t*c1+c2;
else if(this.getField("ins."+i).valueAsString == "o4" &&
this.getField("mins"+i).valueAsString != "")
total += t*d1+d2;
else if(this.getField("ins."+i).valueAsString == "o5" &&
this.getField("mins"+i).valueAsString != "")
total += t*e1+e2;}
if(total == 0)event.value = 0;
else
event.value = total;

Related

Get array values from column

If my function gets the values of one column, say column I, how can I tell it to instead get the values of the column to the right (J) instead of I:K?
function headerSearch(e, activeCell, activeRow, activeCol, data, mode, secMode, terMode) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var foundValues = [];
var forConR = data.length;
var forConC = data[0].length;
Logger.log("data[0] = " + data[0]);
for (var i = 1; i < forConR; i++) {
for (var j = 0; j < forConC; j++) {
if (activeCell != "" && activeCol == 2 && data[0][j].indexOf(mode) > -1) {
if (activeCell.getValue() == data[0][j]) {
foundValues.push(data[i][j]);
}
} else if (activeCell != "" && activeCol == 3 && data[0][j].indexOf(mode) > -1 && data[i][j] != "") {
foundValues.push(data[i][j]);
Logger.log("foundValues = " + foundValues);
}
}
}
if (foundValues != "") {
var validationRule = SpreadsheetApp.newDataValidation().requireValueInList(foundValues).build();
activeCell.offset(0, 1).setDataValidation(validationRule);
}
}
EDIT:
I tried adding foundValues.push(data[i][j+1]); which gets me out of the first column (I), but then of course adds the NEXT column (L) that I don't want either. I'm just not sure how to isolate the column index. Once I figure that out, I'm sure it's just a matter of adding +1 or something to OFFSET to the column to the right.
You have two for loops - one of them iterating through all rows, the second through all columns of data
What you want instead is to retrieve only ONE column of data rather than iterating through ALL of them
You can do it by simply dropping the second for loop and instead hardcoding the value for j
If you are itnerested in the second column of your range - the column index should be 1 (since array indices start with 0)
Without having a deeper knowledge of the purpose of your if conditions and assuming that you use them only to assess the value in column J, you can modify your code as following:
...
for (var i = 1; i < forConR; i++) {
var j = 1;
if (activeCell != "" && activeCol == 2 && data[0][j].indexOf(mode) > -1) {
if (activeCell.getValue() == data[0][j]) {
foundValues.push(data[i][j]);
}
} else if (activeCell != "" && activeCol == 3 && data[0][j].indexOf(mode) > -1 && data[i][j] != "") {
foundValues.push(data[i][j]);
Logger.log("foundValues = " + foundValues);
}
}
...
I rearranged my if statements and added one to isolate the "mode" column (B) selected. At that point, I could add j + 1 to get the following column values for the next data validation selection.
function headerSearch(e, activeCell, activeRow, activeCol, data, mode, secMode, terMode) {
var foundValues = [];
var forConR = data.length;
var forConC = data[0].length;
if (activeCell != "") {
for (var i = 1; i < forConR; i++) {
for (var j = 0; j < forConC; j++) {
if (data[0][j] == mode && data[i][j] != "") {
var modeCol = j;
}
if (activeCol == 2 && data[i][j] != "") {
if (activeCell.getValue() == data[0][j]) {
foundValues.push(data[i][j]);
}
} else if (activeCol == 3 && data[0][j].indexOf(mode) > -1 && data[i][j] != "" && data[0][modeCol + 1].indexOf(mode) > -1) {
foundValues.push(data[i][modeCol + 1]);
} else if (activeCol == 4 && data[0][j].indexOf(mode) > -1 && data[i][j] != "" && data[0][modeCol + 2].indexOf(mode) > -1) {
foundValues.push(data[i][modeCol + 2]);
}
}
}
}
if (foundValues != "") {
var validationRule = SpreadsheetApp.newDataValidation().requireValueInList(foundValues).build();
activeCell.offset(0, 1).setDataValidation(validationRule);
}
}

Creating an array that tells if a given number is smaller than 10

I am trying to make this work, I don't know what is wrong with it.
It used to work with only 3 variables, when I made it longer it stoped and now I cannot make it work even with only 3. Now it gives me "Undefined Undefined Undefined".
What I want is to crate an alarm in a way that it shows the name of whatever has gone under 10. The fields it is comparing from are calculation fields with the REPEATABLESUM() code.
var choice1 = '';
var choice2 = '';
var choice3 = '';
var choice4 = '';
var choice5 = '';
var choice6 = '';
var choice7 = '';
var choice8 = '';
var choice9 = '';
var choice10 = '';
var choice11 = '';
var choice12 = '';
var choice13 = '';
var choice14 = '';
var choice15 = '';
var choice16 = '';
var choice17 = '';
var choice18 = '';
var choice19 = '';
var choice20 = '';
var choice21 = '';
var choice22 = '';
var choice23 = '';
var choice24 = '';
if (NUM($stock_coliblue_24) < 10) {
choice1 = LABEL('coliblue');
}
if (NUM($stock_absorvent_pads) < 10) {
choice2 = LABEL('absorvent_pads');
}
if (NUM($stock_micro_filters) < 10) {
choice3 = LABEL('micro_filters');
}
if (NUM($stock_alkaphotalkalinity) < 10) {
choice4 = LABEL('alkaphot');
}
if (NUM($stock_ammonia) < 10) {
choice5 = LABEL('ammonia');
}
if (NUM($stock_calcicol_calcium_hardness) < 10) {
choice6 = LABEL('calcicol');
}
if (NUM($stock_chloridol_nacl) < 10) {
choice7 = LABEL('chloridol');
}
if (NUM($stock_free_chlorine_dpd1) < 10) {
choice8 = LABEL('chlorine_free_dpd1');
}
if (NUM($stock_total_chlorine_dpd3) < 10) {
choice9 = LABEL('chlorine_total_dpd2');
}
if (NUM($stock_free_copper_n1) < 10) {
choice10 = LABEL('copper_free_n1');
}
if (NUM($stock_total_copper_n2) < 10) {
choice11 = LABEL('copper_total_n2');
}
if (NUM($stock_fluoride) < 10) {
choice12 = LABEL('fluoride');
}
if (NUM($stock_hardicol_total_hardness) < 10) {
choice13 = LABEL('hardicol');
}
if (NUM($stock_iron_hr) < 10) {
choice14 = LABEL('iron_hr');
}
if (NUM($stock_iron_mr) < 10) {
choice15 = LABEL('iron_mr');
}
if (NUM($stock_magnecol_magnesium) < 10) {
choice16 = LABEL('magnecol');
}
if (NUM($stock_manganese) < 10) {
choice17 = LABEL('manganese');
}
if (NUM($stock_nitratest_nitrate) < 10) {
choice18 = LABEL('nitratest');
}
if (NUM($stock_nitricol_nitrite) < 10) {
choice19 = LABEL('nitricol');
}
if (NUM($stock_phosphate) < 10) {
choice20 = LABEL('phosphate');
}
if (NUM($stock_potassium) < 10) {
choice20 = LABEL('potassium');
}
if (NUM($stock_silica) < 10) {
choice21 = LABEL('silica');
}
if (NUM($stock_sulphate) < 10) {
choice22 = LABEL('sulphate');
}
if (NUM($stock_sulphitest_sulphite) < 10) {
choice23 = LABEL('sulphitest');
}
if (NUM($stock_zinc) < 10) {
choice24 = LABEL('zinc');
}
if (choice1 == null && choice2 == null && choice3 == null && choice4 == null && choice5 == null && choice6 == null && choice7 == null && choice8 == null && choice9 == null && choice10 == null && choice11 == null && choice12 == null && choice13 == null && choice14 == null && choice15 == null && choice16 == null && choice17 == null && choice18 == null && choice19 == null && choice20 == null && choice21 == null && choice22 == null && choice23 == null && choice24 == null) {
SETRESULT('');
} else {
CONCAT (choice1 + choice2 + choice3 + choice4 + choice5 + choice6 + choice7 + choice8 + choice9 + choice10 + choice11 + choice12 + choice13 + choice14 + choice15 + choice16 + choice17 + choice18 + choice19 + choice20 + choice21 + choice22 + choice23 + choice24);
}
user array
var choice = [];
var concatStr = "";
if (NUM($stock_coliblue_24) < 10) {
choice[0] = 'coliblue';
}
if (NUM($stock_absorvent_pads) < 10) {
choice[1] = 'absorvent_pads';
}
if (choice.length == 0){
SETRESULT('');
} else {
concatStr = arr.join(" ");
}
var choice = []; /*Using array notation instead of using 24 separate variables*/
var concatStr = "";
if (NUM($stock_coliblue_24) < 10) {
choice[0] = 'coliblue'; /*storing value in array rather than in variables*/
}
,..
,..
if (NUM($stock_zinc) < 10) {
choice[23] = LABEL('zinc');
}
/* Using for loop to iterate through array to check for null values. This is easier than having '==' null check for all 24 variables*/
for(var i=0;i<24;i++) {
if(choice[i] == null) {
SETRESULT('');
break;
}
}
/*If none of the values are null, then concat the array elements. Array.join is so easier than concatenating 24 string variables.*/
if(choice.length==24) {
concatStr = choice.join(" ");
}
Hope this explains.

JavaScript While Loop Variable Returning NaN

I'm making a Numerology conversion web app with JavaScript and HTML5. I've ran into a strange issue with a variable and loops. Below is my whole project in its current form.
<html>
<body>
<h1>My First Numerology Project</h1>
<form>
Month:<br>
<input type="text" maxlength="2" name="userInputMonth" id='userInputMonth'>
<br>
Day:<br>
<input type="text" maxlength="2" name="userInputDay" id='userInputDay'>
<br>
Year:<br>
<input type="text" maxlength="4" name="userInputYear" id='userInputYear'>
<br><br>
First Name:<br>
<input type="text" name="userInputFirstName" id='userInputFirstName'>
<br>
Middle Name:<br>
<input type="text" name="userInputMiddleName" id='userInputMiddleName'>
<br>
Last Name:<br>
<input type="text" name="userInputLastName" id='userInputLastName'>
<br><br>
<input type="button" value="Calculate" name="Calculate" onclick="BirthDayCalculation()"/>
</form>
<p id="LLNResult"></p>
<br>
<p id="OPNResult"></p>
<br>
<p id="SNResult"></p>
<br>
<p id="PODNResult"></p>
<script type="text/javascript">
//Defined var
var LLNTemp1;
var LLNTemp2;
var LLNReduce1;
var LLNReduce2;
var NameTemp1;
var NameTemp2;
var OPNReduce1;
var OPNReduce2;
var SNReduce1;
var SNReduce2;
var PODNReduce1;
var PODNReduce2;
function BirthDayCalculation() {
var Reduction1 = 0;
var LLNReduceFinal2 = 0;
var Day = document.getElementById('userInputDay').value;
var Month = document.getElementById('userInputMonth').value;
var Year = document.getElementById('userInputYear').value;
var FirstName = document.getElementById('userInputFirstName').value;
var MiddleName = document.getElementById('userInputMiddleName').value;
var LastName = document.getElementById('userInputLastName').value;
var LLN = ("" + Day + Month + Year).toString();
var FullName = ("" + FirstName + MiddleName + LastName).toString().toUpperCase();
var FullNameLength = parseInt(FullName.length);
var LLNLength = parseInt(("" + Day + Month + Year).length);
var i = 0;
var LLNTemp2 = 0;
while (i < LLNLength) {
LLNTemp1 = parseInt(LLN.charAt(i));
LLNTemp2 += LLNTemp1;
i++;
}
if (LLNTemp2 > 9) {
var i = 0;
var a = parseInt(LLNTemp2.toString().length);
var LLNReduceFinal1 = 0;
var LLNTemp3String = LLNTemp2.toString();
while (i < a) {
LLNReduce1 = parseInt(LLNTemp3String.charAt(i));
LLNReduceFinal1 += LLNReduce1;
i++;
}
var LLNReduceFinal2 = 0;
var Reduction1 = LLNReduceFinal1;
if (LLNReduceFinal1 > 9) {
var i = 0;
var a = parseInt(Reduction1.toString().length);
var LLNTemp3String = Reduction1.toString();
while (i < a) {
LLNReduce1 = parseInt(LLNTemp3String.charAt(i));
LLNReduceFinal2 += LLNReduce1;
i++;
}
}
}
var OPN = 0;
var SN = 0;
var PODN = 0;
var PODNWhole = 0;
var i = 0;
while (i < FullNameLength) {
NameTemp1 = FullName.charAt(i);
var OPNTemp = 0;
var SNTemp = 0;
if (NameTemp1 == "A") SNTemp = 1;
if (NameTemp1 == "J" || NameTemp1 == "S") OPNTemp = 1;
if (NameTemp1 == "B" || NameTemp1 == "K" || NameTemp1 == "T") OPNTemp = 2;
if (NameTemp1 == "C" || NameTemp1 == "L") OPNTemp = 3;
if (NameTemp1 == "U") SNTemp = 3;
if (NameTemp1 == "D" || NameTemp1 == "M" || NameTemp1 == "V") OPNTemp = 4;
if (NameTemp1 == "E") SNTemp = 5;
if (NameTemp1 == "N" || NameTemp1 == "W") OPNTemp = 5;
if (NameTemp1 == "F" || NameTemp1 == "X") OPNTemp = 6;
if (NameTemp1 == "O") SNTemp = 6;
if (NameTemp1 == "G" || NameTemp1 == "P" || NameTemp1 == "Y") OPNTemp = 7;
if (NameTemp1 == "H" || NameTemp1 == "Q" || NameTemp1 == "Z") OPNTemp = 8;
if (NameTemp1 == "I") SNTemp = 9;
if (NameTemp1 == "R") OPNTemp =9;
OPN += OPNTemp;
SN += SNTemp;
PODNWhole += SNTemp;
PODNWhole += OPNTemp;
i++;
}
var PODN = PODNWhole
var OPNReduceFinal = 0;
if (OPN > 9) {
var OPNLength = parseInt(OPN.toString().length);
var OPNTempString = OPN.toString();
var i = 0;
while (i < OPNLength) {
OPNReduce1 = parseInt(OPNTempString.charAt(i));
OPNReduceFinal += OPNReduce1;
i++;
}
}
var SNReduceFinal = 0;
if (SN > 9) {
var SNLength = parseInt(SN.toString().length);
var SNTempString = SN.toString();
var i = 0;
while (i < SNLength) {
SNReduce1 = parseInt(SNTempString.charAt(i));
SNReduceFinal += SNReduce1;
i++;
}
}
var PODNReduceFinal = 0;
if (PODN > 9) {
var PODNLength = parseInt(PODN.toString().length);
var PODNTempString = PODN.toString();
var i = 0;
while (i < PODNLength) {
PODNReduce1 = parseInt(PODNTempString.charAt(i));
PODNReduceFinal += PODNReduce1;
i++;
}
// var PODNReduceFinal = PODNReduce2;
// var PODN = PODNReduce2;
}
var LLNResult = "Your Life Lesson Number Is:" + LLNTemp2 + "/" + Reduction1;
if (LLNReduceFinal2 != 0) {
LLNResult = LLNResult + "/" + LLNReduceFinal2;
}
var OPNResult = "Your Outer Personality Number is:" + OPN;
if (OPNReduceFinal != 0) {
var OPNResult = OPNResult + "/" + OPNReduceFinal;
}
var SNResult = "Your Soul Number is:" + SN;
if (SNReduceFinal != 0) {
var SNResult = SNResult + "/" + SNReduceFinal;
}
var PODNResult = "Your Path of Destiny Number is:" + PODNWhole;
if (PODNReduceFinal != 0) {
var PODNResult = PODNResult + "/" + PODNReduceFinal;
}
if (LLNTemp2 != 0) {
document.getElementById("LLNResult").innerHTML = LLNResult;
}
if (PODNWhole != 0) {
document.getElementById("OPNResult").innerHTML = OPNResult;
document.getElementById("SNResult").innerHTML = SNResult;
document.getElementById("PODNResult").innerHTML = PODNResult;
}
}
</script>
</body>
</html>
So basically you enter your birthday and full name into a form. the script will then convert the name to numbers A = 1, B = 2.... and reduce both to single digits. the numbers will be added one by one. example. 13=4, 55=10, 63=9.
Here is the general code used to make reductions. Let's make PODN = 55
var PODNReduceFinal = 0;
var PODN = 55;
if (PODN > 9) {
var PODNLength = parseInt(PODN.toString().length);
var PODNTempString = PODN.toString();
var i = 0;
while (i < PODNLength) {
PODNReduce1 = parseInt(PODNTempString.charAt(i));
PODNReduceFinal += PODNReduce1;
i++;
}
}
Currently, it only loops once but I would like to have it get reduced to the lowest number possible. 1 in this case. I was thinking that if I change the if to a while it would accomplish this but it doesn't work the way I thought it would. I tried to make some changes and got stuck in a loop. So I started to troubleshoot. I added another variable to prevent the whole loop from looping infinitely.
var PODNReduceFinal = 0;
var PODN = 55;
while (PODN > 9) {
var PODNLength = parseInt(PODN.toString().length);
var PODNTempString = PODN.toString();
var i = 0;
while (i < PODNLength) {
PODNReduce1 = parseInt(PODNTempString.charAt(i));
PODNReduce2 += PODNReduce1;
i++;
}
var PODNReduceFinal = PODNReduce2;
var PODN = PODNReduce2;
}
Using this I thought when PODN would be checked by the while loop first it would run through the first steps. Get the length of the number, in this case 2. Convert it to a string so it can pull the digits one by one. Pull the digits in sequence with the length of the loop and convert them to numbers. Add them together in PODNReduce2, 55=10. Set PODNReduceFinal = 10, set PODN = 10. Then return to the first condition and check that PODN is now 10 and is greater than 9. Loop again to reduce further. Doing this until it's a single digit stored in PODNReduceFinal. But that's not what happens. When I try to display PODNReduceFinal I get NaN. Even if I revert the first "while" to "if" it still doesn't work. I'm at a loss. Please help.

Refining Data 'Fetch/Match' in Google Scripts

I have some code that is working fine, just rather slow and clunky. Im sure there is a better way of doing this:
I have two sheets, one which the user edits ('MAIN') and one which the script pulls data from (over 5000 lines of data) ('REF'). Basically it checks for a match on column 1 and if it matches what the user has entered, it grabs the value from the same row but column 3.
The code works, but rather slowly:
function onEdit(){
var s = SpreadsheetApp.getActive().getSheetByName('MAIN');
var sref = SpreadsheetApp.getActive().getSheetByName('REF');
var activeRow = s.getActiveCell().getRow();
var activeCol = s.getActiveCell().getColumn();
var activeCell = s.getActiveCell().getValue();
var mainPageBranch = s.getRange(8,1).getValue();
var refPageBranch = sref.getRange('I4').getValue();
var lastrow = sref.getLastRow();
if(activeCol == '2' || activeCol == '5' || activeCol == '8' || activeCol == '11' || activeCol == '14' || activeCol == '17' || activeCol == '20'){
if(activeRow > 9 && activeRow < 41){
if(activeCell > 100000){
s.getRange(activeRow, activeCol+1).setValue(1);
}
else{
s.getRange(activeRow, activeCol+1).setValue('');
}
for(var i=5; i <lastrow; i++){
var productCode = sref.getRange(i, 1).getValue();
if(activeCell == productCode){
var essMould = sref.getRange(i,3).getValue();
s.getRange(activeRow+1,activeCol).setValue(essMould);
s.getRange(activeRow+1,activeCol+1).setValue('1');
break;
}
}
Logger.log('Product Code: ' + productCode);
Logger.log('Ess Mould: ' + essMould);
Logger.log('Last Row: ' + lastrow);
}
}
}
I understand it's kind of clunky - any help making it faster would be great!
Let me know if you need anything else :)
EDIT: here is the updated code thanks!
function onEdit(){
var s = SpreadsheetApp.getActive().getSheetByName('MAIN');
var sref = SpreadsheetApp.getActive().getSheetByName('REF');
var activeRow = s.getActiveCell().getRow();
var activeCol = s.getActiveCell().getColumn();
var activeCell = s.getActiveCell().getValue();
var mainPageBranch = s.getRange(8,1).getValue();
var refPageBranch = sref.getRange('I4').getValue();
var lastrow = sref.getLastRow();
var productCode,essMould,data,L;
data = sref.getRange(5, 1, lastrow-5,3).getValues();
L = data.length; //Length of data
if(activeCol == '2' || activeCol == '5' || activeCol == '8' || activeCol == '11' || activeCol == '14' || activeCol == '17' || activeCol == '20'){
if(activeRow > 9 && activeRow < 41){
if(activeCell > 100000){
s.getRange(activeRow, activeCol+1).setValue(1);
}
else{
s.getRange(activeRow, activeCol+1).setValue('');
}
for(var i=5; i <L; i++){
productCode = data[i][0];
if(activeCell == productCode){
essMould = data[i][2];
s.getRange(activeRow+1,activeCol).setValue(essMould);
s.getRange(activeRow+1,activeCol+1).setValue('1');
break;
}
}
Logger.log('Product Code: ' + productCode);
Logger.log('Ess Mould: ' + essMould);
}
}
}
You should not be getting individual values on every loop:
for(var i=5; i <lastrow; i++){
var productCode = sref.getRange(i, 1).getValue();
First get all the values, loop through the array of data, change the data in the array if needed, and then write the entire set of data back to the spreadsheet.
var data,essMould,i,L,productCode;
data = sref.getRange(5, 1, lastrow-5,activeCol).getValues();
L = data.length; //Length of data
for(i=0; i < L; i++){
productCode = data[i][0];
Logger.log("productCode: " + productCode);
if (activeCell == productCode) {
essMould = data[i][activeCol];
data[i][2] = essMould;
data[i + 1][activeCol + 1] = 1;
break;
}
}
sref.getRange(5,1,data.length,data[0].length).setValues(data);

Assigning static data amount in a div to be called from a function

I'm using an example based on the following example:
http://jsfiddle.net/5tt7d3e6/
In this, a function is created to turn a number into words.
The function is processed in the following HTML
<input type="text" name="number" placeholder="Number OR Amount" onkeyup="word.innerHTML=convertNumberToWords(this.value)" />
<div id="word"></div>
The above allows you to enter a number into a textbox. The function translates what you type as a number into words.
Is there an easy way to set a div which already holds the number, instead of typing it to display?'
Such as:
<div data="innerHTML=convertNumberToWords(1233213)"></div>
You can hookup an event handler for onreadystatechange and inside that you can put your logic.
Fiddle 1
document.onreadystatechange = function() {
word.innerHTML = convertNumberToWords(1233213);
};
However if you want show data based on existing value inside input, first put an id attribute for input
<input id="number" ... />
And then in JS:
document.onreadystatechange = function() {
word.innerHTML = convertNumberToWords(number.value);
};
Fiddle 2
Suggestion: use switch statment in convertNumberToWords()
switch(amount){
case 0: 'zero'; break;
default: 'Please enter number only!'; break;
}
If you want to write to the document as it is processed and not later.
<div id="word">
<script>
document.write(convertNumberToWords(12233456))
</script>
</div>
This is the recommended way though:
<div id="word"></div>
<script>
document.getElementById('word').innerHTML = convertNumberToWords(12233456);
</script>
--
<script>
function convertNumberToWords(amount) {
var words = new Array();
words[0] = '';
words[1] = 'One';
words[2] = 'Two';
words[3] = 'Three';
words[4] = 'Four';
words[5] = 'Five';
words[6] = 'Six';
words[7] = 'Seven';
words[8] = 'Eight';
words[9] = 'Nine';
words[10] = 'Ten';
words[11] = 'Eleven';
words[12] = 'Twelve';
words[13] = 'Thirteen';
words[14] = 'Fourteen';
words[15] = 'Fifteen';
words[16] = 'Sixteen';
words[17] = 'Seventeen';
words[18] = 'Eighteen';
words[19] = 'Nineteen';
words[20] = 'Twenty';
words[30] = 'Thirty';
words[40] = 'Forty';
words[50] = 'Fifty';
words[60] = 'Sixty';
words[70] = 'Seventy';
words[80] = 'Eighty';
words[90] = 'Ninety';
amount = amount.toString();
var atemp = amount.split(".");
var number = atemp[0].split(",").join("");
var n_length = number.length;
var words_string = "";
if (n_length <= 9) {
var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
var received_n_array = new Array();
for (var i = 0; i < n_length; i++) {
received_n_array[i] = number.substr(i, 1);
}
for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
n_array[i] = received_n_array[j];
}
for (var i = 0, j = 1; i < 9; i++, j++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
if (n_array[i] == 1) {
n_array[j] = 10 + parseInt(n_array[j]);
n_array[i] = 0;
}
}
}
value = "";
for (var i = 0; i < 9; i++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
value = n_array[i] * 10;
} else {
value = n_array[i];
}
if (value != 0) {
words_string += words[value] + " ";
}
if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Crores ";
}
if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Lakhs ";
}
if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Thousand ";
}
if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
words_string += "Hundred and ";
} else if (i == 6 && value != 0) {
words_string += "Hundred ";
}
}
words_string = words_string.split(" ").join(" ");
}
return words_string;
}
</script>
<div id="word">
<script>
document.write(convertNumberToWords(12233456))
</script>
</div>
Fiddle Link
change the value of textbox and press click button it will return into word
<input id="check"type="text" name="number" placeholder="Number OR Amount" />
<div id="word"></div>
<input type="button" id="button" value="click">
js code
$("#button").click(function()
{
var number = $('#check').val();
$("#word").html(toWords(number));
});
var th = ['','thousand','million', 'billion','trillion'];
var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine']; var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen']; var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety']; function toWords(s){s = s.toString(); s = s.replace(/[\, ]/g,''); if (s != parseFloat(s)) return 'not a number'; var x = s.indexOf('.'); if (x == -1) x = s.length; if (x > 15) return 'too big'; var n = s.split(''); var str = ''; var sk = 0; for (var i=0; i < x; i++) {if ((x-i)%3==2) {if (n[i] == '1') {str += tn[Number(n[i+1])] + ' '; i++; sk=1;} else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}} else if (n[i]!=0) {str += dg[n[i]] +' '; if ((x-i)%3==0) str += 'hundred ';sk=1;} if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}} if (x != s.length) {var y = s.length; str += 'point '; for (var i=x+1; i<y; i++) str += dg[n[i]] +' ';} return str.replace(/\s+/g,' ');}

Categories

Resources