I'm leaving this post as reference and context for Sam's answer, but you should use his version instead.
Here's your function (in C#) where I took the liberty to add the friction as a parameter instead of hard-coding it:
static float ApplyFriction(float value, float friction, float dt)
return value - value * (1f - friction) * dt;
You might also want to do a speed=Max(0,speed) afterwards.
So you'd like speed to reduce 5% per second instead of per frame? That's easy enough to approximate.
First realize that:
speed = speed * friction;
Is equivalent to:
speed = speed - speed * (1 - friction);
Then simply apply the timestep:
speed = speed - speed * (1 - friction) * dt;
While not giving exactly perfect results because the friction is applied as a percentage of the value during each individual step instead of the initial value at each second, in practice the difference is insignificant, as you may check with this sample test:
http://ideone.com/JkJ2I (with 60 evenly spaced frames)
http://ideone.com/XKfSw (with an unknown number of variable frames adding up to 1 second)
Where an initial speed of 1000 and a friction value of 0.95 yielded:
- 950 when applying it with fixed time step.
- 951.2092 when applying it with variable time step over the course of 60 frames in a second.
That's about 0.1% divergence i.e. they're equivalent for pratical purposes.