Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 14, 2017, 07:34:52 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Byte Formatter  (Read 2695 times)
adamgonzalez1984
Jr. Member
**
Posts: 89


« on: January 13, 2010, 03:39:38 PM »

Code: (Objective-C)
#import "BSByteFormatter.h"

@implementation BSByteFormatter

- (NSString *)stringForObjectValue:(id)anObject
{
  NSArray *sizeTable = [NSArray arrayWithObjects:@"B", @"KB", @"MB", @"GB", @"TB", nil];
   
  int i;
  float result = [anObject floatValue];
 
  if([anObject respondsToSelector:@selector(length)])
  {
    return anObject;
  }
 
  for(i = 0; i < 5 && result >= 1024; i++)
  {
    result = result/1024;
  }
 
  if((float)(result - (int)result) > 0.01)
  {
    return [NSString localizedStringWithFormat:@"%.2f %@", result, [sizeTable objectAtIndex:i]];
  }
 
  else
  {
    return [NSString localizedStringWithFormat:@"%.0f %@", result, [sizeTable objectAtIndex:i]]; 
  }
}

@end
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: January 13, 2010, 07:29:07 PM »

Adam,

Thanks for being the first to post to this new board.  It would be helpful to include a brief description that describes what the code does, as well as its inputs and outputs.  Of course, a little code showing sample usage would be great as well!   Grin

Cheers,

Steve
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.