Objective-C: How to format string as $ Price

前端 未结 4 1882
忘了有多久
忘了有多久 2020-12-05 23:42

Is their a built-in way of formatting string as $ price, e.g. 12345.45 converted to $12,345.45?

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 23:46

    Assuming the price is held in a float, you probably want +localizedStringWithFormat:.

    NSString *priceString = [NSString localizedStringWithFormat:@"$ %'.2f",price];
    

    Hmmm... Apple says they follow the IEEE standard for printf, so it should accept the ' flag, but it doesn't work on Tiger. NSNumberFormatter it is.

提交回复
热议问题