我正在制作一款与货币相关的iOS应用。我的应用程序收到的价值可能是:4美元。应用程序接收的此值将放入NSNumber中。问题是价值实际上有一个美元。如何修剪NSNumber中的$?或者我会把它放到NSString中更好吗?
答案 0 :(得分:1)
使用NSNumberFormatter
:
// set up your number formatter
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
// get a string that you'll be converting to a NSNumber
NSString *myNumberString = [NSString stringWithFormat:@"$4"]
// convert then print to the console
NSNumber *myNumber = [numberFormatter numberFromString:myNumberString];
NSLog(@"myNumber: %@", myNumber);
这应该可以实现您的目标。需要更改myNumberString
以包含您收到的任何字符串。
NSNumberFormatter文档:https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/Reference/Reference.html