While our powerful form builder offers the ability to make calculations with the Formula field, set a minimum/maximum value for a number or change the accepted data format, to round automatically a number you will need to add javascript to the form. Luckily you are just in the right place to learn how to do that!
Let’s take the following script as an example:
function roundResult() {
var result1 = loader.engine.document.getElementById(111111111).getProperty('value.value');
var roundedValue1 = Math.round(result1 * 1) / 1;
loader.engine.document.getElementById(111111111).setValue(({"value": roundedValue1}));
};
window.onclick = roundResult;
This script is used to change a fractional number (a number with decimals) into an integer (a whole number). For example, 3.123 becomes 3, while 3.789 becomes 4.
Change the numbers “111111111” with your own field’s ID (see how to find the field ID at pt. 2 here). Replace with the same field ID in both places, if you want the rounding to be in the same field. If you want to display the fractional number in a field and in another field display its rounded counterpart, then simply add the first field’s ID in place of the first 111111111 and the second field’s ID in the place of the second 111111111.
See how it works:
Let’s say you need to display the prices on your form in a more currency friendly format. You want to avoid numbers such as 25.45678 and display this instead $25.46.
First, make sure you add the currency symbol of your choice with our Prefix option (learn how here).
Second, you will need to use this script on your Formula or Number field:
function roundResult() {
var result1 = loader.engine.document.getElementById(111111111).getProperty('value.value');
var roundedValue1 = +((Math.round(result1 * 100)/100).toFixed(2));
loader.engine.document.getElementById(111111111).setValue(({"value": roundedValue1}));
};
window.onclick = roundResult;
The same instructions apply as for the previous script.
See how it works:
In order to use these scripts on your own form, after you’ve added your own field IDs, paste the URL containing the script file into the Advanced → Form → Add a JS script to your form. See our documentation on how to add JS to your form here.
Note: The script examples provided above trigger the action to round the number when a click is made on the form page. You can however change that. E.g: instead of “window.onclick” you can use “window.onchange” so that the script is triggered when an input is changed in your form.
Here is a list of the most frequently asked questions. For more FAQs, please browse through the FAQs page.