c# - Estimated time remaining, what am I missing? -


i think logic flawed....

in loop have:

int seconds = (int) (elapsed.totalseconds / itempos) * (count - itempos); 

this loop updates approximately once per second....

the problem have seconds ends 0 (0) value.

this because itempos value higher after first loop elapsed.totalseconds.

so example:

if 3 seconds pass

itemcount = 20 , 3/20 = 0.15 - rounds zero.... 0 * = 0...... 

what doing wrong?

integer division returns int, , when result 0.something, it's truncating 0.

make real value, , you're fine. or, multiply before dividing:

int seconds = (int) ((float)elapsed.totalseconds / itempos) * (count - itempos);

(see below)

or

 int seconds = (int) (elapsed.totalseconds * (count - itempos) / itempos); 

edit

based on dtb's comments, realize put typecast in wrongplace. first line won't work (though second still will). what's happening you're taking double value totalseconds , dividing int itempos double. you're casting int, set 0. henk holterman said, have add parenthesis, typecasting on entire division:

 int seconds = (int) ((elapsed.totalseconds / itempos) * (count - itempos)); 

Comments