Math With Formstack - Calculating Number of Cabins
Heidi from Camp Anderson, one of our long time customers, posed a very interesting question recently:
"We rent cabins in our camp. A cabin can fit 10 people. We'd like a field that calculates a total price based on the number of cabins required. Each cabin costs $75 to rent."
This calculation is also known as a step-function in mathematics. From 1-10 people, we want Formstack to calculate 1 cabin, for 11-20 we need 2 cabins and so on.
Normally you could do this by just by dividing the number of people by 10 and then use a rounding function to round down the number of cabins we need, but since Formstack does not have rounding functions we need to try something different. We only have basic math for Formstack calculation functions as you may remember.
Here's what we came up with:
Instead of dividing simply by 10 we take advantage of Formstack's rounding capabilities. Calculation fields round down or up to the nearest number of decimal places setting for a number field. If the number of decimal places is set to 0, your form will round to the nearest integer. But what we need to do is essentially force Formstack to round up in all cases. Adding 0.49 to the division formula makes the result bump up to the nearest integer effectively getting the right number of cabins in all cases.