Amazon.com Widgets Byte Formatter
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 31, 2014, 06:03:58 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Shared Code Library
| | |-+ Byte Formatter
Pages: [1] Go Down
Print
Author Topic: Byte Formatter (Read 1891 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:



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.