Copy link to clipboard
Copied
I have a formula script that I have:
var v1 = Number(this.getField("NMS").value);
var v2 = Number(this.getField("CMS").value);
var v3 = Number(this.getField("CMS").value);
if (v3==0) event.value = "";
else event.value = (v1-v2)/v3;
if (v2==0) event.value = "";
if (v1==0) event.value = "";
They don't want it to round up but they also want me to leave it at two decimals.
Can someone please help me.
Copy link to clipboard
Copied
Set the field's Format to None, and change the end of the code to this:
if (v1==0 || v2==0 || v3==0) event.value = "";
else event.value = ((v1-v2)/v3).toFixed(2);
Copy link to clipboard
Copied
There is no need to use two variables for same field.
toFixed() will still round result.
You can use this:
var v1 = Number(this.getField("NMS").valueAsString);
var v2 = Number(this.getField("CMS").valueAsString);
function twodecimals(v, d) {
return (Math.floor(v * Math.pow(10, d)) / Math.pow(10, d)).toFixed(d);}
if (v1==0 || v2==0) event.value = "";
else
event.value = twodecimals((v1-v2)/v2, 2);