Objective-C - 如何从字符串中删除字符?
我有一个 UILabel,它有一个格式化字符串(针对货币格式化),因此有一个美元符号,$21.34。
在核心数据实体中,属性的类型为 double,我使用 NSDecimalNumber 来保存到数据库。
self.purchase.name = self.nameTextField.text;
NSString *string = self.amountLabel.text
NSDecimalNumber *newAmount = [[NSDecimalNumber alloc] initWithString:string];
NSLog(@"%@", string); // THIS RETURNS NaN, because of dollar sign i think
NSManagedObjectContext *context = self.purchase.managedObjectContext;
NSError *error = nil;
if (![context save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
无论如何,我需要它不为 NaN,所以我的想法是删除美元符号,但我不知道该怎么做,或者也许有更好的方法来实现我的目标。
I have a UILabel that has a formatted String (formatted for currency), so there is a dollar sign, $21.34.
In the core data entity the attribute is of a type double, I am using an NSDecimalNumber to save to the database.
self.purchase.name = self.nameTextField.text;
NSString *string = self.amountLabel.text
NSDecimalNumber *newAmount = [[NSDecimalNumber alloc] initWithString:string];
NSLog(@"%@", string); // THIS RETURNS NaN, because of dollar sign i think
NSManagedObjectContext *context = self.purchase.managedObjectContext;
NSError *error = nil;
if (![context save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
Anyway, I need this to not be NaN, so my thinking is to remove the dollar sign, but i do not know how to do that, or perhaps there is a better way to accomplish my goal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
比上面的强大得多(我认为它也能处理逗号)
Is a lot more robust than the one above (and I think it will handle commas, too)
好的,所以我最终使用了这段代码,它对我有用
OK, so i ended up using this code and it works for me
添加到@justin的答案:
不会删除逗号。因此,您可以将其与: 结合
使用:
获得适合算术的干净数字(例如 £1,005.33 变为 1005.33)
To add to @justin's answer:
won't remove commas. So you can combine it with:
and use:
to get a cleaned number suitable for arithmetic (e.g. £1,005.33 becomes 1005.33)
实际上,您应该拥有自己的价值,它是一个简单的数字,就像交易货币时的浮点数一样。那么你应该有一个代表你的值的字符串。您可以使用 NSNumberFormatter 从值获取字符串,就像您所说的那样,但您不应该依赖字符串作为该值的唯一来源。
另外,NSDecimalNumber 对于表示货币之类的东西可能有点过分了。 NSDecimalNumber 更适合处理科学记数法中的数字。
一个简单的 NSNumber 对象足以处理货币,使用诸如
numberWithDouble:
之类的东西。Really you should have your value which is a simple number, like a float if dealing in currency. Then you should have a string that represents your value. You could use an NSNumberFormatter to get from the value to the string, like you say you have, but you shouldn't be relying on the string as your only source of that value.
Also, NSDecimalNumber is probably overkill for representing something like currency. NSDecimalNumber is better suited for dealing with numbers in scientific notation.
A simple NSNumber object will be good enough for working with currency, using something like
numberWithDouble:
.结合 @justin 和 @leafy 的答案:
这会给你 1000。
Combining @justin's and @leafy's answer:
This will give you 1000.
如果需要删除$符号。为什么不在处理数字之前执行以下操作
If you need to remove the $ sign. why not just do the following before dealing with the number
试试这个:
Try this: