Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Don't want it to Round up??

New Here ,
Apr 28, 2023 Apr 28, 2023

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. 

TOPICS
JavaScript
717
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 28, 2023 Apr 28, 2023

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);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 29, 2023 Apr 29, 2023
LATEST

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);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines