How to make a process last a specific amount of time?

Ok, i’m making a value to go from X to Y in intervals of 1. I.E:
X = 10, Y = 8
Display: 10, 9, 8

Every one of those numbers take 0.1 seconds to change. It looks good with small values, but with bigger ones (such as, X = 10000, Y = 7800) it takes far too long. Is it possible to make the process always in, for example, 5 seconds? So it takes the same amount of time to go down from 10 to 8 and 10000 to 7800?

Yes! First you’ll need to know what X and Y are. In your examples you gave X=10, Y=8 and X=10,000, Y=7,800. So we will use those.

Next you’ll need a new float to keep track of how fast to decrement on each interval, something like float percentChange = .10f will do for now.

Then create another float called float valueChange; and set it equal to the difference of X and Y multiplied by the percentChange so valueChange = (X - Y) * percentChange;

Then set Display -= valueChange;

This should decrement 10 by .20, and 10,000 by 220. Making sure no matter the X and Y values it will always decrement by the specified percentChange in this case 10%.

To confirm the math you can take (10 - 8) / .20 = 10 intervals and (10,000 - 7,800) / 220 = 10 intervals. The 10 intervals is coming from our percentChange which is 10%.