2

I have a method that creates a money style string from an NSNumber.

+ (NSString *)formatShortPayment: (NSNumber *)payment {

    if (![payment isEqual: [NSNull null]]) {

        // Set locale to GB
        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        [formatter setLocale: locale];
        [formatter setNumberStyle: NSNumberFormatterCurrencyStyle];

        // If the payment is 1000 or over, get rid of fraction numbers
        if ([payment doubleValue] >= 1000.0) {
            [formatter setMaximumFractionDigits: 0];
            payment = [NSNumber numberWithFloat:([payment floatValue] / 1000)];
        }

        else {
            // Here's where I want to check if the value ends with .00 or not
        }

        // Return clean number
        return [formatter stringFromNumber: payment];

    }

    return nil;
}

This works great, but if the value is < 1000.0 I'd like to check what the trailing digits are and remove them if they equal .00.

For example: 25.00 => 25 13.29 => 13.29

I'm not sure how best to accomplish this. Any suggestions?

4

1 回答 1

10
if (([payment doubleValue] - floor([payment doubleValue]) < 0.01) {
    [formatter setMaximumFractionDigits: 0];
}

This should work.

于 2012-11-25T15:37:47.163 回答