6 * Cheesy number->string conversion, complete with carry rounding error.
8 char *num2str(unsigned long num, int maxlen, int base, int pow2)
10 char postfix[] = { ' ', 'K', 'M', 'G', 'P', 'E' };
11 unsigned int thousand[] = { 1000, 1024 };
12 unsigned int modulo, decimals;
19 for (post_index = 0; base > 1; post_index++)
20 base /= thousand[!!pow2];
23 while (post_index < sizeof(postfix)) {
24 sprintf(tmp, "%lu", num);
25 if (strlen(tmp) <= maxlen)
28 modulo = num % thousand[!!pow2];
29 num /= thousand[!!pow2];
35 sprintf(buf, "%lu%c", num, postfix[post_index]);
39 sprintf(tmp, "%lu", num);
40 decimals = maxlen - strlen(tmp);
44 sprintf(fmt, "%%.%uu", decimals - 1);
45 sprintf(tmp, fmt, modulo);
46 sprintf(buf, "%lu.%s%c", num, tmp, postfix[post_index]);