Expand my Community achievements bar.

SOLVED

Simple formcalc Avg calculation default value issue

Avatar

Level 3

I have a simple Avg FormCalc calculation below, but the default value is showing -0.01 and for the life of me I do not know why, and need some help.

 

Avg(pmp.Page8.D1, pmp.Page8.D2, pmp.Page8.D3, pmp.Page8.D4, pmp.Page8.D5, pmp.Page8.D6, pmp.Page8.D7, pmp.Page8.D8, pmp.Page8.D9, pmp.Page8.D10 ,pmp.Page8.D11, pmp.Page8.D12, pmp.Page8.D13, pmp.Page8.D14 ,pmp.Page8.D15 ,pmp.Page8.D16)

I even tried adding the following code to see if it would help, but didn't.

if (this.rawValue <= 0) this.rawValue = "";

The only other thing I did to this field was check the limit field box, and change the display pattern to "num{zzzz9.zz}"

 

Any help would be appreciated.

1 Accepted Solution

Avatar

Correct answer by
Level 5

To round down, in FormCalc, you could use this formula:

round(NumericField1.rawValue + .005,2)-.01

View solution in original post

9 Replies

Avatar

Level 5

I don't know why the avg function would return -0.01 as default, but I think the test you tried is scripted in JavaScript, not in FormCalc and I doubt you can mix both languages in the same event.

Avatar

Level 3

I know the second piece of the code is javascript, but the top i did in formcalc.  its weird since the code @Mayank_Tiwari  has, is the same exact code i have in the calculate script, except for a few misplaced commas, which I fixed.  But, I found the issue where the second "overall score" field was also doing an average calculation for some reason, and once I took that out, it fixed the issue.

 

But now my issue is that the 2nd field, no matter what I do with javascript, is showing a 0 by default, but I want it to show blank, any ideas on this?

commalliance_1-1644330920652.png

 

 

 

Avatar

Level 3

So, I was testing the code for these drop down fields and I realized that is was rounding up to the 2nd decimal, for example 4.745 was displaying as 4.75.  So, I simply added - .005 to the end of the calculation, so now by default the field is displaying -0.01 by default.  I have been trying various codes for example:

if (this.rawValue != null) then
""
endif

I have tried a few other things like:

 

this.rawValue = PBoverall.value.toFixed(2)

but anything that I do to either set the default value as null or 0, ends up with the field not pulling the data from the field doing the calculation.

 

I'm really struggling with this here, because although the calculation works as expected when the fields are filled out, they do not want to have the default showing -0.01

Avatar

Correct answer by
Level 5

To round down, in FormCalc, you could use this formula:

round(NumericField1.rawValue + .005,2)-.01

Avatar

Level 3

Thank you so much for your help, as I am learning a lot.

I actually thought maybe that I could do a divide calculation within the formula you gave me to achieve my end result, but quickly found that I couldn't, so I ended up having to do this code, which seems to be working correctly

 

this.rawValue = Avg(overall_score2, overall_score1)
round(this.rawValue + .005,2)-.01

since I need to get that average but keep it from rounding up.  So the few tests I have done, I have backed up with using a calculator, but was just wondering so i can learn, if there might be a simpler way of doing something like this in the future, to get the same results?

Avatar

Level 5

I guess you can combine both lines:

this.rawValue = round(Avg(overall_score2, overall_score1)+.005,2)-.01

Designer seems to only offer rounding to the closest decimal, whereas you needed to round down to the decimal below. So I don't think you can escape doing a little math!

Avatar

Level 3

Ok, great, I didn't realize you could combine that, but thank you for that information.