Created
April 24, 2014 15:21
-
-
Save mortymacs/11258602 to your computer and use it in GitHub Desktop.
convert number to human-readable size in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
char *types[] = {"KB","MB","GB","TB","PB","EB","ZB","YB"}; | |
void change(int *size) | |
{ | |
int div = 1024; | |
int i = 0; | |
for(;i<8;i++) | |
{ | |
*size /= div; | |
if(*size < div) | |
{ | |
printf("%d %s\n",*size,*(types+i)); | |
break; | |
} | |
} | |
} | |
void main() | |
{ | |
int size = 2048; | |
change(&size); | |
size = 225614; | |
change(&size); | |
size = 225614000; | |
change(&size); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment