如何将带有数字的字符串转换为带有逗号而不是小数点的 NSDecimalNumber ?

发布于 2024-09-29 15:42:37 字数 947 浏览 4 评论 0原文

我有一个界面给我这样的数字 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥抱影子 2024-10-06 15:42:37

要将 NSNumber 转换为 NSDecimalNumber,使用此代码完全避免字符表示不是更有意义吗?

NSNumber* source = ...;

NSDecimalNumber* result = [NSDecimalNumber decimalNumberWithDecimal:[source decimalValue]]; 

To convert an NSNumber to NSDecimalNumber, wouldn't it make more sense to avoid the character representation altogether with this code?

NSNumber* source = ...;

NSDecimalNumber* result = [NSDecimalNumber decimalNumberWithDecimal:[source decimalValue]]; 
电影里的梦 2024-10-06 15:42:37

如果您查看 NSDecimal类参考,您将看到可以从 NSString(带或不带区域设置)、实际数字等创建新的 NSDecimalNumber

如果您愿意要将 NSNumber 转换为 NSDecimalNumber,您可以执行以下操作:

NSDictionary *locale = ...;
NSNumber *number = ...;
NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithString:[number descriptionWithLocale:locale] locale:locale];

当然,您必须正确创建区域设置等,但这是剩下的练习由您决定(查看 NSNumber 类参考NSLocale 类参考,以及 区域设置编程指南)。

If you check out the NSDecimal Class Reference, you'll see you can create new NSDecimalNumbers from NSStrings (with and without a locale), actual numbers, etc.

If you wanted to convert an NSNumber to an NSDecimalNumber, you could do something like this:

NSDictionary *locale = ...;
NSNumber *number = ...;
NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithString:[number descriptionWithLocale:locale] locale:locale];

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).

笙痞 2024-10-06 15:42:37
[NSDecimalNumber decimalNumberWithString:@"0000000012345,78"];

不过,请谨慎对待语言环境;如果您在区域格式未设置为法语的 iPhone 上运行该代码,它可能不会返回您期望的结果。所以你可能想使用:

+ (NSDecimalNumber *)decimalNumberWithString:(NSString *)numericString locale:(NSDictionary *)locale

代替。

[NSDecimalNumber decimalNumberWithString:@"0000000012345,78"];

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:

+ (NSDecimalNumber *)decimalNumberWithString:(NSString *)numericString locale:(NSDictionary *)locale

instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文