ResKnife/Cocoa/Classes/SizeFormatter.m

46 lines
850 B
Mathematica
Raw Normal View History

2001-10-19 19:41:13 +00:00
#import "SizeFormatter.h"
@implementation SizeFormatter
- (void)awakeFromNib
{
[self setFormat:@"#,##0.0"];
[self setLocalizesFormat:YES];
[self setAllowsFloats:YES];
}
- (NSString *)stringForObjectValue:(id)obj
{
NSMutableString *string = [NSMutableString string];
float value = [obj floatValue];
int power = 0;
while( value >= 1024 /*&& power <= 30*/ )
2001-10-19 19:41:13 +00:00
{
2002-02-23 03:40:24 +00:00
power += 10; // 10 == KB, 20 == MB, 30+ == GB
2001-10-19 19:41:13 +00:00
value /= 1024;
}
switch( power )
{
case 0:
[string appendFormat:NSLocalizedString(@"%.0f", nil), value];
2001-10-19 19:41:13 +00:00
break;
case 10:
[string appendFormat:NSLocalizedString(@"%.1f KB", nil), value];
2001-10-19 19:41:13 +00:00
break;
case 20:
[string appendFormat:NSLocalizedString(@"%.1f MB", nil), value];
2001-10-19 19:41:13 +00:00
break;
default:
[string appendFormat:NSLocalizedString(@"%.1f GB", nil), value];
2001-10-19 19:41:13 +00:00
break;
}
return string;
}
@end