3 ms·
Oh, that makes sense, although I'm not sure "1000.0 kB" is strictly wrong in this case. With the loop at least it's easy to adjust the thresholds if that is de
by jandrese 7y ago
Oh, that makes sense, although I'm not sure "1000.0 kB" is strictly wrong in this case.
With the loop at least it's easy to adjust the thresholds if that is desirable, although a comment will be necessary to explain why you are making the cutoffs in weird places.
Interestingly enough in the past I've used a loop similar to this:
static char suffix[] = { ' ', 'k', 'm', 'g', 't' };
magnitude = 0;
while ( value > 1000 )
{
value /= 1000;
magnitude++;
}
printf("%.1f%cB", value, suffix[magnitude]);
Which is bad because it's subject to repeated rounding from the division, but avoids the problem you described.
- LorenPechtel 7y agoI don't think the multiple divisions will do any harm as you only need 4 digits of precision left at the end.