objc

Currency string from a number (double)

Converting a decimal number to a currency string is simple with NSNumberFormatter.

In our app we deal with money, virtual money.
We needed a simple formatter that would format double:


54.36233 --> 54.36
26.00    --> 26
+ (NSString *)currencyStringFromNumber:(double)number
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    //remove .00 if needed
    if (number == (int)number) {
        [formatter setMaximumFractionDigits:0];
    }
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    NSNumber *aNumber = [NSNumber numberWithDouble:number];
    return [formatter stringFromNumber:aNumber];
}
Currency string from a number (double)

Subscribe to The infinite monkey theorem

Get the latest posts delivered right to your inbox