Skip to content

Instantly share code, notes, and snippets.

@Kozlov-V
Created September 1, 2015 21:32
Show Gist options
  • Save Kozlov-V/fc12e2111930012e4553 to your computer and use it in GitHub Desktop.
Save Kozlov-V/fc12e2111930012e4553 to your computer and use it in GitHub Desktop.
StringValueBytes
- (NSString *)readableValueWithBytes:(id)bytes{
NSString *readable;
//round bytes to one kilobyte, if less than 1024 bytes
if (([bytes longLongValue] < 1024)){
readable = [NSString stringWithFormat:@"1 KB"];
}
//kilobytes
if (([bytes longLongValue]/1024)>=1){
readable = [NSString stringWithFormat:@"%lld KB", ([bytes longLongValue]/1024)];
}
//megabytes
if (([bytes longLongValue]/1024/1024)>=1){
readable = [NSString stringWithFormat:@"%lld MB", ([bytes longLongValue]/1024/1024)];
}
//gigabytes
if (([bytes longLongValue]/1024/1024/1024)>=1){
readable = [NSString stringWithFormat:@"%lld GB", ([bytes longLongValue]/1024/1024/1024)];
}
//terabytes
if (([bytes longLongValue]/1024/1024/1024/1024)>=1){
readable = [NSString stringWithFormat:@"%lld TB", ([bytes longLongValue]/1024/1024/1024/1024)];
}
//petabytes
if (([bytes longLongValue]/1024/1024/1024/1024/1024)>=1){
readable = [NSString stringWithFormat:@"%lld PB", ([bytes longLongValue]/1024/1024/1024/1024/1024)];
}
return readable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment