Skip to main content
Participant
June 20, 2023
Question

Field Calculation adding decimal places where it shouldn't

  • June 20, 2023
  • 1 reply
  • 384 views

On the attached form there is some wierd rounding issues occuring that I can't figure out. 

Line 7 and 8 are both numbers to two decimal places. 

 

Line 9 uses the following formula:

var a = this.getField("A").value;

var b = this.getField("B").value;

if (a !=="" || b !==""){

event.value = b-a;

} else {

event.value ="";

}

 

value of a is 5,112,068.82

value of b is 5,184,600.47

 

For some reason line 9 calculates to 72531.64999999944.

I am having the same issues with line 10. 

 

Can someone please take a look and help me out? I attached the PDF for reference. 

 

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
June 20, 2023
The reason why this is happening is complicated (you can google "JavaScript
floating point precision" to get a better understanding), but the solution
is simple.
Change this line:
event.value = b-a;
To:
event.value = (b-a).toFixed(2);