Comments for BSByteFormatter http://codebeach.org/code/show/4 Comments feed for BSByteFormatter en-us Sat, 01 Mar 2008 17:30:20 +0000 Chris Ryland says: http://codebeach.org/code/show/4 codebeach.org:comment:150 Perhaps too nit-picky, my DRY &quot;code smell sense&quot; suggests you replace that last if with something like the following, &quot;hoisting out&quot; the small difference in those two returns: <br /> <br />NSString *format; <br /> <br />if ((float) (result - (int) result) &gt; 0.01) <br /> format = @&quot;%.2f %@&quot;; <br />else <br /> format = @&quot;%.0f %@&quot;; <br />return [NSString localizedStringWithFormat:format, result, [sizeTable objectAtIndex:i]]; <br /> <br /><br /><br /> Wed, 16 Sep 2009 03:58:47 +0000 Quinn Taylor says: http://codebeach.org/code/show/4 codebeach.org:comment:157 Concise is always good. In my own adapted code, I took it a few steps further... My version accepts a string or an NSNumber, and does away with the counter variable. (The for-in loop works on 10.5 and higher.) Thanks for the ideas! <br /> <br />- (NSString*) stringForIntegerValue:(NSInteger)integerValue { <br /> NSArray *suffixes = [NSArray arrayWithObjects:@&quot;B&quot;, @&quot;KB&quot;, @&quot;MB&quot;, @&quot;GB&quot;, @&quot;TB&quot;, @&quot;PB&quot;, nil]; <br /> float value = integerValue; <br /> for (NSString *suffix in suffixes) { <br /> if (value &gt;= 1024) { <br /> value /= 1024; <br /> } else { <br /> NSString *format = (value - (int)value &gt;= 0.1) ? @&quot;%.1f %@&quot; : @&quot;%.0f %@&quot;; <br /> return [NSString localizedStringWithFormat:format, value, suffix]; <br /> } <br /> } <br />} <br /> <br />- (NSString*) stringForObjectValue:(id)anObject { <br /> return [self stringForIntegerValue:[anObject integerValue]]; <br />}<br /><br />