The result of formula field currenty does not have format field.
For example the calculation result 123456789, it will be written 123456789

It will be nice if it follows the format decimal just like in number field, displayed and saved as 123,456,789

Comments

kyleheney’s picture

Version: 7.x-2.0-alpha1 » 7.x-2.0-alpha2
Component: User interface » Code
Category: Feature request » Bug report

Hi,

I would also like to see this type of functionality. I am using the Formula field on an invoice and the result is only one decimal place (or no decimal places for whole numbers). It looks "off" when a total dollar amount only has one decimal place.

I think this is actually an issue with the module because a user can choose the "Precision" of the formula, but that only acts as a "limit" and not as a default view. It would be better if the "Precision" field would add a zero for any significant digits that don't "exist".

Katy Jockelson’s picture

I agree that it would be great to change the format of what shows when you have selected 2 in the precision dropdown.

i.e 8 should render as 8.00 and 4.5 should be 4.50.

Any chance of implementing this?

kruser’s picture

I imagine it should have the same options as a Number field (since it's calculations are based on them), ex prefixes, decimal places, thousands seperator, etc.

I'm totaling up dollar amounts and need a formatted total.

rafalenden’s picture

Title: Formula format field » Formatting formula result
Version: 7.x-2.0-alpha2 » 7.x-2.x-dev
Category: Bug report » Feature request
kyleheney’s picture

Hi,

Any update on this issue? I've created several more forms, but the totals look very unprofessional because they are dollar amounts that sometimes only have 1 decimal place.

Thanks

rafalenden’s picture

Hi kyleheney,
at the moment I'm busy with other stuff, however any patches are welcome.

kyleheney’s picture

Okay, I've been able to format the formula result so that it displays 2 decimal places on the form itself (in "real-time")

Issue now is that the Submission results do not use the same formatting. They are still showing the improper formatting.

Here's what I've done so far:

Add this code webform_calculator.js (I put it just above the Round section near the end; line 138ish)

 Drupal.webformCalculator.precision = function CurrencyFormatted(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

Then on line 110, change
formulaResult = Drupal.webformCalculator.round(formulaResult, formulaComponent.extra.precision);
to
formulaResult = Drupal.webformCalculator.precision(formulaResult, formulaComponent.extra.precision);

This works to change the format on the form from, for example, "5" to "5.00". On the submission results pages though, the format is still "5".

Any guidance/ideas to move this formatting over into the results?

Edit: I realize this "solution" does not provide an option for setting the precision, but this is how I would use this module, so I didn't take that into consideration. I don't think this would ever get pushed as a committed option for the module, as it doesn't give full functionality, but I'd still like to come up with a solution for my use case so that others can choose to implement it if they are using the formula fields in a similar way.

capmex’s picture

To display two decimal places is easier to change the following line on webform_calculator.js:

formulaComponentElement.text(formulaResult);

to

formulaComponentElement.text(formulaResult.toFixed(2));

kyleheney’s picture

The suggestion in #8 does not work. No change is observed after making this change. The result of the formula still does not show two decimal places on the Webform Results pages.

kyleheney’s picture

I think the solution lies somewhere outside the webform_calculator.js file.... any edits I make to the JS file are only affecting the form elements prior to submission. Once submitted, the values are "static" and do not format correctly (50.00 on the form will show as 50 in submission results).

Is there any way to target the submission results to make sure they should the same precision? It doesn't make sense that I can set the precision of the formula on the form, but the results don't respect this choice of precision.

joel_osc’s picture

Here is a patch that adds the decimal, point, separator and exclude zero configuration to the formula component. I have also added a call in the theme function to _webform_number_format() which handles formatting the number according to the configs.

@kyleheney - would it be possible for you to look at the JS and integrate these configs into the JS code so that webform form render follows the same logic?

kyleheney’s picture

I can confirm this patch works and adds the necessary decimal and zeros when desired. THANK YOU

@joel_osc I'm not sure what is required in the JS code. I added some code in #7 that seems to work well for me. My knowledge of JS is limited and I actually found this example through multiple online searches.

Let me know if there is anything else I can do.

joel_osc’s picture

I think the JS logic needs to match the logic in the theme code that I wrote. I looked at your JS in #8 and it needs a bit of work. I will have our JS guy take a look at it.

kyleheney’s picture

I definitely welcome any changes or additions to what I posted. I realize what I provided was a "forced" addition and didn't provide options for the end user, so I can definitely see how it could be improved. Happy again to test any patch that is provided.

Can't wait to see these changes hopefully added to the next release of this module. They're very useful in my opinion.

aastrong’s picture

StatusFileSize
new4.87 KB

Hey there, sorry for the long wait, had other things on my plate.
So I was able to get this working by mostly using an existing jquery plugin, and a small bit of basic js.

Please take a look at my patch ( which includes @joel_osc's patch )
I've also included the JS plugin file that I added to do this.

The jquery plugin can also be found here
https://github.com/customd/jquery-number

Results seem to be working and consistent for me.

penone’s picture

Having an issue with patch in #15:

I have 3 calculation fields:
1. Subtotal (calculates the weight entered by the price per lb)
2. Tax (calculated by multiplying the subtotal by .16)
3. Total (calculated by adding subtotal + tax.

If my subtotal goes over 999.00 the tax and total no longer calculate.

Not sure why this is doing this. Anyone else running into this?

aastrong’s picture

@penone Hey there, I haven't run into the issue - but I'll try to set up a similar scenario and do some testing in the next few days.

penone’s picture

So I believe the problem lies with the comma. If I select "none" as a separator option the word "none" is actually shown in the field. (ex. 2none000.00 for 2000.00).

Not sure how to go about fixing this.

aastrong’s picture

Hey there, sorry im just looking at this now.
The issue was that value none was set to "none' instead of ''

attaching patch again - please lemme know if this works for you.

Cheers.

vladsavitsky’s picture

There is working patch which does even more:
https://www.drupal.org/node/2652174

vladsavitsky’s picture

Status: Active » Closed (duplicate)