Skip to main content
JMMurphy181
Participant
June 5, 2017
Answered

Help with rounding writing a function for rounding down.

  • June 5, 2017
  • 3 replies
  • 1333 views

I need some help creating a formula for rounding down a field in a form I'm creating.  I need the field to take the numbers from 2 separate fields and add them together subtract 10 and then divide by 2.

Something like (Field1+Field2-10)/2  and then round down to the nearest whole number so 4.5 goes to 4 and so on.

Also I dont know if it would work differently but if it does i need it to work with negative numbers so -3.5 rounds to 4

If anyone could help me with this i would be very appreciative.

This topic has been closed for replies.
Correct answer try67

Try this code as the field's custom calculation script:

var result = ((Number(this.getField("Field1").value)+Number(this.getField("Field2").value))-10)/2;

event.value = Math.round(Math.abs(result));

3 replies

JMMurphy181
Participant
June 5, 2017

Worked out perfectly, thanks for all the help guys.

Legend
June 5, 2017

Or did you mean that -3.5 rounds to -4?

JMMurphy181
Participant
June 5, 2017

Yes, so -3.5 to -4

Inspiring
June 5, 2017

You might have to use the Math.floor method and not the Math.round method.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 5, 2017

Try this code as the field's custom calculation script:

var result = ((Number(this.getField("Field1").value)+Number(this.getField("Field2").value))-10)/2;

event.value = Math.round(Math.abs(result));

JMMurphy181
Participant
June 5, 2017

This kinda works, is there a way to make it work for negative numbers, like -3.5 to -4

Thanks btw for the help.

try67
Community Expert
Community Expert
June 5, 2017

That is more tricky because by default the rounds negative numbers that end with .5 down, while positive numbers get rounded up... You will need to use a custom rounding function to do that.

And to keep the negative numbers negative just remove the Math.abs() part of the code I posted above.