Skip to main content
JMMurphy181
Participant
Jun 5, 2017 10:33 AM UTC
Answered

Help with rounding writing a function for rounding down.

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Jun 5, 2017 11:58 AM UTC

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));

8 replies

JMMurphy181
Participant
Jun 5, 2017 7:12 PM UTC

Worked out perfectly, thanks for all the help guys.

Translate
Legend
Jun 5, 2017 1:20 PM UTC

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

Translate
JMMurphy181
Participant
Jun 5, 2017 1:46 PM UTC

Yes, so -3.5 to -4

Translate
Inspiring
Jun 5, 2017 3:58 PM UTC

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

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Jun 5, 2017 11:58 AM UTC

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));

Translate
JMMurphy181
Participant
Jun 5, 2017 1:58 PM UTC

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

Thanks btw for the help.

Translate
try67
Community Expert
Community Expert
Jun 5, 2017 2:11 PM UTC

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.

Translate