Calculating Percentages
Kieron_59
12 Sept 2012, 14:34Quest isn't calculating percentages right. An example: 25 / 50 * 100 = 0. When I try to do it, quest always rounds down to the nearest hundred. I was able to recreate the problem in a new project and have attached it here with a few more examples in it.
The Pixie
12 Sept 2012, 14:39This is a common programming error, I think (well, it has got me a few times). The problem is that it is working with integers (whole numbers). It does 25 / 50 first, determined the answer in 0.5, but as this is integer arithmatic, it rounds it down to 0, which, multiplied by 100, is 0.
Solution one: Use a decimal
25.0 / 50 * 100 will force it to do floating point arithmetic, which will get the right answer
Solution two: Multiple first
25 * 100 / 50 will also work, and give a percentage as a whole number too, which may be an advantage
Solution one: Use a decimal
25.0 / 50 * 100 will force it to do floating point arithmetic, which will get the right answer
Solution two: Multiple first
25 * 100 / 50 will also work, and give a percentage as a whole number too, which may be an advantage
Kieron_59
12 Sept 2012, 15:04The second solution should work, I didn't know you could do that with math. Thank you.