Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Need help to build a script to divide two integer fields

giovanni3
Kilo Contributor

How do you divide two integer fields to get a value in a separate field?   For example, field1=10, field2=5.   I want to divide field1 from field2 and have the value in field3.

11 REPLIES 11

var StartNumber = '444,555,666';


var ReplacedNumber = StartNumber.replace(/\,/g,'');


document.write(ReplacedNumber);



The above shows how a regular expression can be used in JavaScript to remove the commas.


giovanni3
Kilo Contributor

I did change the field C from integer to decimal.


giovanni3
Kilo Contributor

I also tried changing field A and B from integer to decimal but it made no difference.


giovanni3
Kilo Contributor

Hi Tim,


Forgive me but I cannot program in javascript!   Do I add that to the onload script?   If so, what lines should I add them in?   Below is the onload client script...



function onLoad() {
    //do nothing, just loading the function below
}


function divide_a_by_b() {  
      var a = parseInt(g_form.getValue('u_total_test_count___ca'),10);  
      var b = parseInt(g_form.getValue('u_total_req_count___ca'),10);  
      var result = 0; // force to zero if infinite  
      if (b != 0) {  
              var result = (a/b).toFixed(2);  
      }  
      g_form.setValue('u_avg_test_req_count___ca',result);  
}  


Try:



function onLoad() {
    //do nothing, just loading the function below
}


function divide_a_by_b() {


      var first = g_form.getValue('u_total_test_count___ca');


      first = first.replace(/\,/g,'');


      var second = g_form.getValue('u_total_req_count___ca');


      second = second.replace(/\,/g,'');


      var a = parseInt(first,10);  


      var b = parseInt(second,10);


      var result = 0; // force to zero if infinite  
      if (b != 0) {  
              var result = (a/b).toFixed(2);  
      }  
      g_form.setValue('u_avg_test_req_count___ca',result);  
}



Hopefully my cutting and pasting didn't break something.   I don't know that you still need to do the parseInt.