When working with calculations in your FormAssembly forms, you often want to present results in a clear and concise format. This is especially true for calculations involving money, percentages, or measurements. Rounding the answer to a specific number of decimal places gives a polished presentation and avoids unnecessary decimals that might confuse users. Plus it’s actually quite easy and requires only a little javascript.
Let’s dive into a practical example and define the variables
Imagine you’re creating a form for an event planning service. You want to calculate the cost per person based on the total cost and the number of attendees. Here’s how rounding can enhance the user experience:
- Variables: Define variables for the total cost (“
totalCost
“) and the number of attendees (“numAttendees
“). - Calculation: Use the formula
(totalCost / numAttendees).toFixed(2)
to calculate the cost per person and round the result to two decimal places. This ensures the user sees a clear price without unnecessary decimals (e.g., $25.75 instead of $25.7500).
Our first variable is the number of people.
Let’s work on our second example in the form. First, make sure you’ve defined the variables you want to use in the calculation. Give them meaningful names so you can easily remember what they represent. For this example, let’s consider that we’re planning a trip. We know how many people are coming and the total cost of the trip, so we want to find the cost per person and round it to the nearest cent.
Next, define the total cost variable.
FormAssembly’s capabilities:
FormAssembly empowers you with various calculation functions beyond basic arithmetic. You can explore formulas for:
- Rounding:
.toFixed(n)
rounds to n decimal places. - Floor and Ceiling:
Math.floor(x)
rounds down to the nearest integer, whileMath.ceil(x)
rounds up. - Advanced Math Functions: Utilize functions like
Math.sin
,Math.cos
, andMath.pow
for more complex calculations.
By incorporating rounding and other calculation features, you can create user-friendly forms that present data in a clear and professional manner.
Adding the calculation
Create your calculation field, then determine the formula you need to use. In this case, that will be cost/people. To round to two decimal places, add .toFixed(2)
to the end of the formula, so the final formula looks like this:(cost/people).toFixed(2)
Beyond cost per person: rounding use cases
Rounding calculations aren’t limited to cost. Here are some other common scenarios:
- Inventory management: Rounding inventory quantities to whole numbers or specific units (e.g., boxes, pallets) simplifies stock management.
- Discount calculations: When applying discounts as percentages, rounding the resulting amount to the nearest cent avoids minor discrepancies.
- Average scores: Rounding average scores (e.g., test results, customer satisfaction surveys) to a single decimal point provides a clear and concise overview.
Other calculations
FormAssembly enables you to use many different calculations on your forms. For more information, see our previous post on calculations and repeatable sections.