开发者

Objective C display money format like Sensible Soccer

I'm wanting to display money like SWOS (or Sensible World of Soccer) used to.

IE: Instead of: $10,0开发者_开发百科00,000+ you got $10m, 10.5m, etc.

Instead of: $1,000,000 you got $1m

Instead of: $1,500,000 you got $1.5m

It also worked for both large and smaller figures, say; 1k, 1.25k, 0.75k, 0.25k, etc.

I'm wondering, is there a way to display money in a format that is similar to the way SWOS used to do it?

Thanks.


You can do this by writing a subclass of either NSFormatter or NSValueTransformer, depending on your needs. If you want to use the values together with bindings you should go with NSValueTransformer. If the values are user-editable and need to perform validation, use NSFormatter.

Here is a quick example of the guts of an NSFormatter:

- (NSString *) stringForObjectValue:(id)anObject
{
    if(!anObject || ![anObject respondsToSelector:@selector(doubleValue)])
        return nil;

    NSTimeInterval interval = [anObject doubleValue];

    double intervalInSeconds = fabs(interval);
    double intervalInMinutes = round(intervalInSeconds / SECONDS_PER_MINUTE);

    if(intervalInMinutes >= 0 && intervalInMinutes <= 1) {
        if(!self.includeSeconds)
            return (intervalInMinutes == 0 ? NSLocalizedString(@"less than a minute", @"") : NSLocalizedString(@"1 minute", @""));
        if(intervalInSeconds >= 0 && intervalInSeconds <= 4)
            return NSLocalizedString(@"less than 5 seconds", @"");
        else if(intervalInSeconds >= 5 && intervalInSeconds <= 9) 
            return NSLocalizedString(@"less than 10 seconds", @"");
        else if(intervalInSeconds >= 10 && intervalInSeconds <= 19) 
            return NSLocalizedString(@"less than 20 seconds", @"");
        else if(intervalInSeconds >= 20 && intervalInSeconds <= 39) 
            return NSLocalizedString(@"half a minute", @"");
        else if(intervalInSeconds >= 40 && intervalInSeconds <= 59) 
            return NSLocalizedString(@"less than a minute", @"");
        else 
            return NSLocalizedString(@"1 minute", @"");
    }
    else if(intervalInMinutes >= 2 && intervalInMinutes <= 44) 
        return [NSString stringWithFormat:NSLocalizedString(@"%.0f minutes", @""), intervalInMinutes];
    else if(intervalInMinutes >= 45 && intervalInMinutes <= 89) 
        return NSLocalizedString(@"about 1 hour", @"");
    else if(intervalInMinutes >= 90 && intervalInMinutes <= 1439) 
        return [NSString stringWithFormat:NSLocalizedString(@"about %.0f hours", @""), round(intervalInMinutes / MINUTES_PER_HOUR)];
    else if(intervalInMinutes >= 1440 && intervalInMinutes <= 2879) 
        return NSLocalizedString(@"1 day", @"");
    else if(intervalInMinutes >= 2880 && intervalInMinutes <= 43199) 
        return [NSString stringWithFormat:NSLocalizedString(@"%.0f days", @""), round(intervalInMinutes / 1440.0)];
    else if(intervalInMinutes >= 43200 && intervalInMinutes <= 86399) 
        return NSLocalizedString(@"about 1 month", @"");
    else if(intervalInMinutes >= 86400 && intervalInMinutes <= 525599) 
        return [NSString stringWithFormat:NSLocalizedString(@"%.0f months", @""), round(intervalInMinutes / 43200.0)];
    else if(intervalInMinutes >= 525600 && intervalInMinutes <= 1051199) 
        return NSLocalizedString(@"about 1 year", @"");
    else
        return [NSString stringWithFormat:NSLocalizedString(@"over %.0f years", @""), round(intervalInMinutes / 525600.0)];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜