8 Replies Latest reply on Jun 5, 2017 12:12 PM by JMMurphy181

# 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.

• ###### 1. Re: Help with rounding writing a function for rounding down.

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));
```
1 person found this helpful
• ###### 2. Re: Help with rounding writing a function for rounding down.

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

• ###### 3. Re: Help with rounding writing a function for rounding down.

Yes, so -3.5 to -4

• ###### 4. Re: Help with rounding writing a function for rounding down.

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

Thanks btw for the help.

• ###### 5. Re: Help with rounding writing a function for rounding down.

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.

• ###### 6. Re: Help with rounding writing a function for rounding down.

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

1 person found this helpful
• ###### 7. Re: Help with rounding writing a function for rounding down.

That would work, but it would round down any number... So 3.99 will become 3.0. I'm not sure that's what the OP is after.

1 person found this helpful
• ###### 8. Re: Help with rounding writing a function for rounding down.

Worked out perfectly, thanks for all the help guys.