dividing two variables

Phil O_shea
Mega Contributor

Hi guys,

I have been trying to modify a script i originally used to calculate a division of 3 fields i have created.

since then the business owner has changed their mind and want to remove the calculation of 1 of those fields.

this is the script i am currently using:

function onChange(control, oldValue, newValue, isLoading) {

    var distanceHomeToWork = Number(g_form.getValue("u_distance_home_to_work"));

    var totalDistanceTravelled = Number(g_form.getValue("u_total_distance_travelled"));

    var distanceToReimburse = Number(totalDistanceTravelled - distanceHomeToWork);

if (distanceToReimburse != "NaN" && distanceToReimburse > 0) {

    var reimbursementAmount = distanceToReimburse * .66;

//g_form.setValue("u_total_claimable", reimbursementAmount); - keep this line commented out.

var x = reimbursementAmount;

g_form.setValue("u_total_claimable", x.toFixed(2));

}

}

The screenshot below shows what i now need to calculate:

find_real_file.png

Sorry, this is probably super simple but i am just not seeing it.

I tried:

function onChange(control, oldValue, newValue, isLoading) {

  //do nothing, just loading the function below

}

function divide_a_by_b() {

      var a = parseInt(g_form.getValue('u_total_distance_travelled'),10);

      var b = parseInt(g_form.getValue('u_amount'),10);

      var result = 0; // force to zero if infinite

      if (b != 0) {

              var result = (a/b).toFixed(2);

      }

      g_form.setValue('u_total_claimable',result);

}  

and:

function onChange(control, oldValue, newValue, isLoading) {

  //do nothing, just loading the function below

  }

  function divideBy(){

  var totaldist = Number(g_form.getValue('u_total_distance_travelled')).value;

  var amount = Number(g_form.getValue('u_amount'))* .66;

  g_form.setValue('u_total_claimable') = totaldist / amount;

  }

1 ACCEPTED SOLUTION

Hi Philip,



Hope this works:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue === '') {


  return;


  }



  //Type appropriate comment here, and begin script below


  var result = 0; // force to zero if infinite


  var a = g_form.getValue('u_total_distance_travelled');


  var b = g_form.getValue('u_amount');



  if (b != 0) {


  var numobj = parseInt(a) / parseFloat(b);


  result = numobj.toFixed(2);


  }


  g_form.setValue('u_total_claimable',result);


}


View solution in original post

5 REPLIES 5

Shishir Srivast
Mega Sage

Hi Philip,



Can you please try with below code.



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue === '') {


  return;


  }



  //Type appropriate comment here, and begin script below


  var result = 0; // force to zero if infinite


  var a = g_form.getValue('u_total_distance_travelled');


  var b = g_form.getValue('u_amount');



  if (parseInt(b) != 0) {


  var numobj = parseInt(a) / parseInt(b);


  result = numobj.toFixed(2);


  }


  g_form.setValue('u_total_claimable',result);


}


well i get a reaction in the total but it is however only providing '0'



find_real_file.png


Hi Philip,



Hope this works:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue === '') {


  return;


  }



  //Type appropriate comment here, and begin script below


  var result = 0; // force to zero if infinite


  var a = g_form.getValue('u_total_distance_travelled');


  var b = g_form.getValue('u_amount');



  if (b != 0) {


  var numobj = parseInt(a) / parseFloat(b);


  result = numobj.toFixed(2);


  }


  g_form.setValue('u_total_claimable',result);


}


Worked a treat!!!! Thank you.