• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

using calc element to create a percentage total

Racmitch

Member
Hi Guys been awhile, I have a question I'm hoping can be answered in this forum, if I need to bump up to the pro forum let me know. I have used the calc element to create different math calculations, but I need to make one now that will show the difference of 2 numbers as a percentage. I do know to have ajax observe the fields to be used. example:
{product_mapping_summary_block___tottime2},
{product_mapping_summary_block___tottime3},
And I have created the code to place into the "calculation" block within the element,
example: return (real)'{product_mapping_summary_block___tottime2_raw}' + (real)'{product_mapping_summary_block___tottime3_raw}' ;

I just don't know what formula to use to make this end up with a percentage difference showing in "total"
Can someone help me please?
 
Hmmm, according to our internals, you already are Pro? I do hope this doesn't mean our expiration code is fubared, and I've been providing Pro level support to people's whose subs have expired.

The way to get a percentage difference is to get the difference between the two, "new - original", divide by the original number, then multiply by 100 ...

Code:
return ((float)'{product_mapping_summary_block___tottime2}' - (float)'{product_mapping_summary_block___tottime3}') / (float)'{product_mapping_summary_block___tottime2}' * 100;

-- hugh
 
thanks Hugh greatly appreciated. Just to clear up my subscription. It did lapse I could not post on the pro forum and I renewed and paid today to start again. So as far as my account is concerned it is working correctly. oh btw started playing keys in the band....loving it!!!!
 
another question please, kinda struggling. I want to add 5 observed fields and then divide that by 60, in this case to get a minutes to hours conversation.... say the 5 fields are 60+ 60+ 60+ 60+60 which is a total of 300 then divide by 60 to get 5 I tried doing it this way

return (real)'{product_map_test_2___time1_raw}' + (real)'{product_map_test_2___time2a_raw}' + (real)'{product_map_test_2___time3a_raw}' +
(real)'{product_map_test_2___time4a_raw}' + (real)'{product_map_test_2___time5a_raw}'/ 60;

I get a total after calculation of 241, I don't know where that came from instead of getting a total of 5, any ideas?
 
Google PEMDAS.

So it's doing the 60 / 60 first. Then doing the additions.

Put parens around your additions.

Sent from my HTC6545LVW using Tapatalk
 
ok...changed the formula a bit, I added the parens and also changed (real) to (float) to get this and it works correctly....thank you
return ((float)'{product_map_test_2___time1_raw}' + (float)'{product_map_test_2___time2a_raw}'+ (float)'{product_map_test_2___time3a_raw}' + (float)'{product_map_test_2___time4a_raw}' + (float)'{product_map_test_2___time5a_raw}') / 60 ;
 
ok, one more issue, then I'll stop being a pest. I need one more calculation and this one has me completely stumped. I understand it's very difficult to use a "total" field as a numerator to make a calculation, so all observed fields that are not a "total" have to be used. This is the actual calculation with the sample numbers i'm using to make work with the fabrik calc plugin : 275 - 39 ? 275 x 100 - 100 = 14.1 % . The number 275 is an addition of 5 fields that equal 275. This is the code I wrote that I thought was correct, but it does nothing when live on the site:

return ((float)'{product_map_test_2___time1}'+ (float)'{product_map_test_2___time2a}'+ (float)'{product_map_test_2___time3a}'+ (float)'{product_map_test_2___time4a}'+ (float){product_map_test_2___time5a}') - (float)'{product_map_test_2___time1}' / ((float)'{product_map_test_2___time1}'+ (float)'{product_map_test_2___time2a}'+ (float)'{product_map_test_2___time3a}'+ (float)'{product_map_test_2___time4a}'+ (float){product_map_test_2___time5a}') * 100 - 100;

Where am I messing up, or is just my line of thinking complete off base?
 
Again, you probably need to think PEMDAS:

Parentheses
Exponents
Multiplication, Division (left to right order)
Addition, Subtraction (left to right order)

So the way PHP (or any math parser) will interpret this without parens:

275 - 39 / 275 x 100 - 100

Is in PEMDAS order:

(276 - ((39 / 275) x 100)) - 100 = 160

If you want to change that order, you need to put the parens in the right place. The only way I can get your formula to come out is actually -14.1%, as ...

(((276 - 39) / 275) * 100) - 100

So, applying that paren ordering to your actual calc code.

Code:
return
(
   (
      (
         (
            (float)'{product_map_test_2___time1}'+
            (float)'{product_map_test_2___time2a}'+
            (float)'{product_map_test_2___time3a}'+
            (float)'{product_map_test_2___time4a}'+
            (float){product_map_test_2___time5a}'
         )
         -
         (float)'{product_map_test_2___time1}'
      ) 
      /
      (
        (float)'{product_map_test_2___time1}'+
        (float)'{product_map_test_2___time2a}'+
        (float)'{product_map_test_2___time3a}'+
        (float)'{product_map_test_2___time4a}'+
        (float){product_map_test_2___time5a}'
      )
   )
   * 100
)
- 100;

It's often easier to format things like this, PHP doesn't care, and it makes it easier to visualize.

-- hugh
 
Thanks Hugh and I agree I need to learn and apply the pemdas and parens better and correctly. I was under the impression that i needed to double paren just the calcs made with in the whole calculation, like the 5 field addition in this one, but i do see what you did here and it makes sense to me. thanks again for all your help
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top