Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Sheetworker Math problem

July 08 (5 years ago)
Ray
Pro

Greetings all.

I have a calculation problem in the following sheetworker and I need some help with the solution syntax please...

    on('change:body change:str change:con change:stunxp sheet:opened', function () {

        getAttrs(['body','str','con','stunxp'], function(v) {

            const body = v.body *1||0;

            const str = v.str *1||0;

            const con = v.con *1||0;

            const stunxp = v.stunxp *1||0;

            const stun = Math.round(body + str/2 + con/2 + stunxp);

            setAttrs({

                 stun: stun,

                 stuncurrent_max: stun

            });

        });

    });

My error is occurring with the "stun" value...

My input values are: "body"=10  "str"=11  "con"=11  "stunxp"=0 

I am getting a final "stun" value of 21....it should be 22.

The error appears to be in the rounding...I believe the following calculation is occurring:

10 + 5.5 + 5.5 + 0 - 21

I need it to be:

10 + 6 + 6 + 0 = 22

In other words....the 11 / 2 needs to round...

How do I achieve this?

Thanks in advance...

July 08 (5 years ago)
Ray
Pro

Ok...admins please delete this thread...I solved the problem.

July 08 (5 years ago)
Andreas J.
Forum Champion
Sheet Author
Translator

Could you share your solution here, even if you figured out yourself? It might help someone else in the future if they find this thread looking for answers.

July 08 (5 years ago)
Ray
Pro


Andreas J. said:

Could you share your solution here, even if you figured out yourself? It might help someone else in the future if they find this thread looking for answers.

Sure...

I pre-calculated the "str/2" and "con/2" by adding "Math.round" instructions into the sheetworker prior to the final "Math.round"...and I created two new variables "stra" and "cona"...

    on('change:body change:str change:con change:stunxp sheet:opened', function () {

        getAttrs(['body','str','con','stunxp'], function(v) {

            const body = v.body *1||0;

            const str = v.str *1||0;

            const con = v.con *1||0;

            const stunxp = v.stunxp *1||0;

            const stra = Math.round(str/2);

            const cona = Math.round(con/2);

            const stun = Math.round(body + stra + cona + stunxp);

            setAttrs({

                 stun: stun,

                 stuncurrent_max: stun

            });

        });

    });

This resulted in the formula changing to (body + (str/2) + (con/2) + stunxp) which gave me the correct result  :)