سؤال

As the title states, I'd like to start from 1 and decrement with by 0.01 all the way down to zero.

Problem is, I'm using floats and I keep getting values such as 0.5000000001.

هل كانت مفيدة؟

المحلول

Simply use an int, Start at 100 decrement down to 0 and divide the value by 100.0

for (int i=100; i>=0; --i)
{    
    float f = i/100.0f;
    ...
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top