如何将带有数字的字符串转换为带有逗号而不是小数点的 NSDecimalNumber ?
我有一个界面给我这样的数字 0000000012345,78
所以我想出了如何用它们来生成一个数字。但我需要用这个数字进行计算,而我实际需要的是一个十进制数。
NSNumberFormatter *fmtn = [[NSNumberFormatter alloc] init];
[fmtn setFormatterBehavior:NSNumberFormatterBehavior10_4];
[fmtn setNumberStyle:NSNumberFormatterDecimalStyle];
[fmtn setDecimalSeparator:@","];
NSNumber *test = [fmtn numberFromString:@"0000000012345,78"];
如何将我的 NSNumber 转换为 NSDecimalNumber?
编辑:这是我最终得到的代码:
NSDictionary *localeDict = [NSDictionary dictionaryWithObject:@"," forKey:@"NSDecimalSeparator"];
NSDecimalNumber *test = [[NSDecimalNumber alloc] initWithString:@"00000000012345,78" locale:localeDict];
如何将语言环境字典放在一起不能被描述为“有据可查”,我花了一些谷歌搜索才能找到一个例子。
这也有效:
NSLocale *deLoc = [[NSLocale alloc] initWithLocaleIdentifier:@"de"];
NSDecimalNumber *testd = [NSDecimalNumber decimalNumberWithString:@"00000000012345,78" locale:deLoc];
I have an interface giving me numbers like this 0000000012345,78
So i figured out how to make a number out of them. But I need to calculate with that number and what I actually need is a decimal number.
NSNumberFormatter *fmtn = [[NSNumberFormatter alloc] init];
[fmtn setFormatterBehavior:NSNumberFormatterBehavior10_4];
[fmtn setNumberStyle:NSNumberFormatterDecimalStyle];
[fmtn setDecimalSeparator:@","];
NSNumber *test = [fmtn numberFromString:@"0000000012345,78"];
How can I make my NSNumber to a NSDecimalNumber?
EDIT: This is the code I ended up with:
NSDictionary *localeDict = [NSDictionary dictionaryWithObject:@"," forKey:@"NSDecimalSeparator"];
NSDecimalNumber *test = [[NSDecimalNumber alloc] initWithString:@"00000000012345,78" locale:localeDict];
How to put together the locale dictionary could not be described as "well documented" and it took me some googling to find an example.
This one also worked:
NSLocale *deLoc = [[NSLocale alloc] initWithLocaleIdentifier:@"de"];
NSDecimalNumber *testd = [NSDecimalNumber decimalNumberWithString:@"00000000012345,78" locale:deLoc];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要将
NSNumber
转换为NSDecimalNumber
,使用此代码完全避免字符表示不是更有意义吗?To convert an
NSNumber
toNSDecimalNumber
, wouldn't it make more sense to avoid the character representation altogether with this code?如果您查看 NSDecimal类参考,您将看到可以从
NSString
(带或不带区域设置)、实际数字等创建新的NSDecimalNumber
。如果您愿意要将
NSNumber
转换为NSDecimalNumber
,您可以执行以下操作:当然,您必须正确创建区域设置等,但这是剩下的练习由您决定(查看 NSNumber 类参考,NSLocale 类参考,以及 区域设置编程指南)。
If you check out the NSDecimal Class Reference, you'll see you can create new
NSDecimalNumber
s fromNSString
s (with and without a locale), actual numbers, etc.If you wanted to convert an
NSNumber
to anNSDecimalNumber
, you could do something like this:Of course, you'll have to correctly create the locale, and such, but that's an exercise left up to you (it might be handy to check out the NSNumber Class Reference, the NSLocale Class Reference, and the Locales Programming Guide).
不过,请谨慎对待语言环境;如果您在区域格式未设置为法语的 iPhone 上运行该代码,它可能不会返回您期望的结果。所以你可能想使用:
代替。
Use caution about the locale, though; if you run that code on an iPhone whose region format is not set to French, it might not return what you expect. So you might want to use:
instead.