# [Resolved] Another question about calculations

This support ticket is created 9 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This topic contains 4 replies, has 2 voices.

Last updated by dbarber 9 years, 3 months ago.

Assigned support staff: Bigul.

Author
Posts
#175304

I need to do a slightly more complex calculation than discussed over here https://toolset.com/forums/topic/need-to-limit-the-number-of-digits-output-from-a-calculation/

Basically, I need to do a calculation like this:

(A - B) / B

And I need it to output as a percentage limited to 2 decimal points.

I've tried to work it out on my own, but haven't had any luck so far.

Any help would be greatly appreciated.

Thanks

Dawson

#176085

Bigul
Supporter

Dear Dbarber,

Easy solution will be you can write a custom function for this purpose. Then pass A & B as arguments. Then calculate (A – B) / B inside the custom function.

If you are still facing the problem, please share the function and short code you are using. Also describe little more about your custom fields.

--
With Regards

Bigul

#176191

Hi Bigul

I still need a solution for this, but I'm not sure how to do what you've advised so I can't share a function or shortcode.

Here's more detail on the fields I need to evaluate:

[types field="detached-sales-current" raw="true" id=""][/types] minus
[types field="detached-active-listings-current" raw="true" id=""][/types] divided by [types [field="detached-active-listings-current" raw="true" id=""][/types]

And I need it to output as a percentage, limited to 2 decimal points, and, if the result is less than 0.00, I need the output to be colored red.

Thanks for your assistance with this.

Cheers

Dawson

#176454

Bigul
Supporter

Dear Dawson,

Hope you are trying to achieve the following.

[types field="detached-sales-current" raw="true" id=""][/types] = A

[types field="detached-active-listings-current" raw="true" id=""][/types] = B

The formula is (A - B) / B

You can write a custom function for this purpose like the following.

```add_shortcode('wpv-formula-calculate', 'formula_calculate');
function formula_calculate(\$atts) {
\$result = wpv_condition(\$atts);
if(\$result < 0){
return '<font color="red">'.number_format(\$result,2)."%</font>";
}else{
return number_format(\$result,2)."%";
}
}
```

Then call it inside views like the following.

[wpv-formula-calculate evaluate='([types field="detached-sales-current"][/types] - [types field="detached-active-listings-current"][/types])/ [types field="detached-active-listings-current"][/types]']

--
With Regards

Bigul

#176741

Hi Bigul

I realized that I haven't done a good job asking for help because I have a few support threads open that I realized cannot be resolved independent of one another so I decided to start a fresh thread that combines all them.

Here's the new "all in one" thread: https://toolset.com/forums/topic/need-to-perform-4-different-calculations-on-the-same-website/

I really appreciate your excellent support and apologize for having taken up your time. I should have thought this through more fully before asking for assistance.

Cheers

Dawson