The gform_polls_percentage_precision Gravity Forms PHP function allows you to round the percentages on the front-end Polls results to a custom number of decimal digits. By default, the percentage is rounded to the nearest whole number.
Usage
add_filter('gform_polls_percentage_precision', 'your_function_name', 10, 2);
Parameters
- $precision (int): The number of decimal digits to use.
- $form_id (int): The form id.
More information
See Gravity Forms Docs: gform_polls_percentage_precision
Examples
Set custom precision to 1 decimal place
This example sets the custom precision for Polls results to display 1 decimal place.
add_filter('gform_polls_percentage_precision', 'gform_polls_custom_precision', 10, 2); function gform_polls_custom_precision($precision, $form_id) { return 1; }
Set custom precision based on form ID
This example sets the custom precision for Polls results to display 2 decimal places for a specific form with ID 5.
add_filter('gform_polls_percentage_precision', 'gform_polls_custom_precision_based_on_form_id', 10, 2); function gform_polls_custom_precision_based_on_form_id($precision, $form_id) { if ($form_id == 5) { return 2; } return $precision; }
Set custom precision for all forms except form with ID 3
This example sets the custom precision for Polls results to display 3 decimal places for all forms except the form with ID 3.
add_filter('gform_polls_percentage_precision', 'gform_polls_exclude_precision_for_form_id', 10, 2); function gform_polls_exclude_precision_for_form_id($precision, $form_id) { if ($form_id != 3) { return 3; } return $precision; }
Set custom precision only for forms with specific IDs
This example sets the custom precision for Polls results to display 4 decimal places only for forms with IDs 7 and 9.
add_filter('gform_polls_percentage_precision', 'gform_polls_precision_for_specific_forms', 10, 2); function gform_polls_precision_for_specific_forms($precision, $form_id) { if (in_array($form_id, array(7, 9))) { return 4; } return $precision; }
Set custom precision based on user role
This example sets the custom precision for Polls results to display 5 decimal places for logged-in users with the ‘editor’ role.
add_filter('gform_polls_percentage_precision', 'gform_polls_precision_for_user_role', 10, 2); function gform_polls_precision_for_user_role($precision, $form_id) { if (current_user_can('editor')) { return 5; } return $precision; }