使用 NSNumberFormatter 格式化已有小数和千位分隔符的数字

发布于 2024-10-07 10:33:13 字数 783 浏览 2 评论 0原文

我有这些数字格式:

100000,459
100000459
100.000
100.000,59
100.000.000,39

数字随着用户输入值而变化。对于每个添加的值,我需要使用 NSNumberFormatter 重新格式化数字。问题是这个数字已经有了。和 ,并且格式化程序似乎无法正确处理这些。结果是这样的:

100 000,00
100 000 459,00
100,00
100,00
100,00

例如,我希望 100000,459 变成 100 000,459。

我现在使用的代码:

NSNumber *amount = [NSNumber numberWithInt:[string intValue]];
NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"nb_NO"];
[currencyFormatter setLocale:locale];
NSString *commaString = [currencyFormatter stringForObjectValue:amount];

如何格式化已经格式化的数字?

I have these number formats:

100000,459
100000459
100.000
100.000,59
100.000.000,39

The number changes as the user input values to it. And for every value added, I need to re-format the number with NSNumberFormatter. The problem is that this number already has . and , and the formatter does not seem to handle these correctly. It comes out like this:

100 000,00
100 000 459,00
100,00
100,00
100,00

E.g. I want 100000,459 to become 100 000,459.

The code I use now:

NSNumber *amount = [NSNumber numberWithInt:[string intValue]];
NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"nb_NO"];
[currencyFormatter setLocale:locale];
NSString *commaString = [currencyFormatter stringForObjectValue:amount];

How can I format already formatted numbers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

杀手六號 2024-10-14 10:33:13

在最后一行中,您尝试格式化字符串而不是数字。试试这个:

NSString *commaString = [currencyFormatter stringFromNumber:amount];

这里有一个很好的参考

In the last line you are trying to format a string rather than a number. Try this:

NSString *commaString = [currencyFormatter stringFromNumber:amount];

Here is a good reference.

素年丶 2024-10-14 10:33:13

我看过那个具体的参考资料。它与我的问题不同,因为就我而言,格式化时可能已经有小数了。

I've looked at that specific reference. It differs from my problem because in my case I may already have decimals when formatting.

神魇的王 2024-10-14 10:33:13

NSNumberFormatter数字格式化为字符串表示形式,并且它需要数字的正确表示形式才能按预期格式化它。从代码示例的外观来看,您正在为格式化程序提供一个整数值,这就是您获得输出的原因。使用 string = @"100000,459" [string intValue] 可以得到 100000。这就是格式化程序的输出为 100 000,00 的原因。

您需要做的是首先将字符串转换为双精度表示形式。这可以通过使用 NSScanner 来实现。您只需确保扫描正确区域设置中的数字,以便正确检测千位分隔符。您的第一个字符串可以通过以下方式转换为双精度:

NSScanner *scanner = [NSScanner scannerWithString:@"100000,459"];
[scanner setLocale:locale];
double val;
[scanner scanDouble:&val];
NSNumber *amount = [NSNumber numberWithDouble:val];

如何正确处理我不知道的千位分隔符。

NSNumberFormatter formats a number to a string representation and it needs the correct representation of the number to be able to format it as expected. By the look of your code sample you are providing the formatter with an integer value and that is why you are getting the output you are getting. With string = @"100000,459" [string intValue] gives you 100000. Which is why the output from the formatter is 100 000,00.

What you need to do is to first get the string into a double representation. That can be achieved by using NSScanner. You just have to make sure that you scan the numbers in the correct locale so the thousand separators are detected properly. Your first string can be converted to a double by:

NSScanner *scanner = [NSScanner scannerWithString:@"100000,459"];
[scanner setLocale:locale];
double val;
[scanner scanDouble:&val];
NSNumber *amount = [NSNumber numberWithDouble:val];

How to properly handle the thousand separators I don't know.

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