Is NSNumberFormatter the only way to format an NSDecimalNumber?

后端 未结 4 1289
借酒劲吻你
借酒劲吻你 2020-12-31 11:20

I\'m using an NSDecimalNumber to store money in Core Data. I naively used stringWithFormat: at first to format the value, later realizing that it didn\'t support NSDecimalNu

相关标签:
4条回答
  • 2020-12-31 12:03

    Just convert it to a double and use "F"

    [NSString stringWithFormat:@"%1.2F", [ent doubleValue]];

    0 讨论(0)
  • 2020-12-31 12:09

    on iOS 4.0 and later you can use this method:

    NSString* currencyString = [NSNumberFormatter 
                                     localizedStringFromNumber:number
                                     numberStyle:NSNumberFormatterCurrencyStyle];
    
    0 讨论(0)
  • 2020-12-31 12:10

    There's nothing wrong with that approach, and it's the conventional pattern to convert opaque number classes to strings.

    If I have a UIView that uses a formatter often, I'll usually have one as an instance member so I avoid having to repeatedly alloc/init/release the formatter.

    0 讨论(0)
  • 2020-12-31 12:15

    NSNumberFormatter can be expensive, why not just call [ent descriptionWithLocale:]

    0 讨论(0)
提交回复
热议问题