Show Two Decimals for Calculation Results

Always show two decimals in your calculation results (e.g. 1.00, 1.50).

Most often if you need to show two decimals it’s because you’re working with currency; however, this isn’t always the case. In these scenarios, this snippet can help.

Instructions

  1. Install this snippet with our free Custom JavaScript plugin. https://gravitywiz.com/gravity-forms-code-chest/

  2. Follow the inline comments to configure for your form and field.

Code

Filename: gw-show-two-decimals-for-calc-results.js

/**
 * Gravity Wiz // Gravity Forms // Show Two Decimals for Calculation Results
 * https://gravitywiz.com/
 *
 * Always show two decimals in your calculation results (e.g. `1.00`, `1.50`).
 *
 * Most often if you need to show two decimals it's because you're working with currency; however, this
 * isn't always the case. In these scenarios, this snippet can help.
 *
 * Instructions:
 *
 * 1. Install this snippet with our free Custom JavaScript plugin.
 *    https://gravitywiz.com/gravity-forms-code-chest/
 *
 * 2. Follow the inline comments to configure for your form and field.
 */
gform.addFilter( 'gform_calculation_format_result', function ( formattedResult, result, formulaField, formId, calcObj ) {
	// Change "4" to your field ID.
	if ( formId == GFFORMID && formulaField.field_id == 4 ) {
		var currency = new Currency( gf_global.gf_currency_config );
    	formattedResult = currency.numberFormat( result, 2, currency.currency.decimal_separator, currency.currency.thousand_separator );
  	}
	return formattedResult;
} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.