percent calculation with counter
Hi,
I am trying to do something that seems extremely straightforward but for some reason is not working. I am trying to calculate a percentage and have a counter always display the percentage. It is for an educational game and I have set up global values for number correct and number incorrect. I create the following event:
Always Set Counter to... global value for number correct/(global value for number correct+global value for number wrong)x 100. This should work but it keeps returning a zero whenever there is a wrong answer. Why? The calculation seems very straightforward. It's just a percentage. Any ideas? Thanks,
Brett
Re: percent calculation with counter
This is quite a common issue - the short answer is that MMF, like many coding languages, only works in non-whole numbers if you tell it to. What's happening with that expression is that the division is done first, and rounded to 0 - then the multiplication has no effect. This will work:
(Correct * 100) / (Correct + Wrong)
As the multiplication is done first, you don't get the rounding error during the division.
Alternatively, if you multiply the value by 1.0 before doing anything to it, then the answer will come out as a decimal (floating-point) value:
(Correct * 1.0) / (Correct + Wrong) * 100
Re: percent calculation with counter
Thanks David,
Your suggestion worked perfectly. Have a great day.
Brett
Re: percent calculation with counter
Hi,
I've noticed another strange thing. Upon initial testing everything was working well but I noticed that after answering several questions correctly the percentage would randomly drop to 99%, 96% or some other high percentage. No questions had been answered incorrectly so I cannot figure out why there would be a small random calculation error. Any ideas? Thanks,
Brett
Re: percent calculation with counter
Check the correct and wrong values in the debugger to see what they are. Maybe correct and wrong are not what you expect them to be. DavidN's equations should both work.
Re: percent calculation with counter
Hi Eliyahu,
I tried them in the debugger and using counters. The values displayed are correct. I agree that the equation should work, but it seems to randomly not work. I'm not sure why. Please let me know if you have any other ideas. Thanks,
Brett
Re: percent calculation with counter
Try to always include .0 in all numbers. For example if you multiply by 100 make it 100.0 and so on. If you have any fixed whole number that will not change but has the possibility of becoming a decimal or returning a decimal, do it always with .0 at the end. It's something weird I found out about MMF...
Re: percent calculation with counter
Hi Geohound,
Thanks. Adding the .0 at the end and limiting the number of significant digits fixed the problem. Thanks,
Brett
Re: percent calculation with counter
I saw your message this morning - it's good to know you got help :) I imagine that in some cases you were ending up with a 33.3% that was being multiplied up to 99.9% and then rounded down, or something like that.
Re: percent calculation with counter