设置数字格式以显示逗号和/或美元符号

发布于 2024-10-25 21:59:46 字数 276 浏览 5 评论 0原文

我想用逗号或更好的美元符号和逗号(没有小数)来格式化我的 UILabel。

这是我正在使用的代码:

IBOutlet UILabel *labelrev

float rev = (x + y)

labelrev.text = [[NSString alloc] initWithFormat:@%2.f",rev];

我得到 xxxxxxxxx 作为输出 我想要得到 xxx,xxx,xxx 或 $xxx,xxx,xxx

我该怎么做?

I want to format my UILabel with commas or better with a dollar sign and commas (with no decimal).

Here is the code I am using:

IBOutlet UILabel *labelrev

float rev = (x + y)

labelrev.text = [[NSString alloc] initWithFormat:@%2.f",rev];

I get xxxxxxxxx as the output I want to get xxx,xxx,xxx or $xxx,xxx,xxx

How do I do that?

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

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

发布评论

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

评论(3

莳間冲淡了誓言ζ 2024-11-01 21:59:46

您绝对应该使用 NSNumberFormatter 为此。基本步骤是:

  1. 分配、初始化和配置您的数字格式化程序。
  2. 使用格式化程序从数字返回格式化字符串。 (它需要一个 NSNumber,因此您需要将您的 double 或任何原语转换为 NSNumber。)
  3. 清理。 (你知道,内存管理。)

此代码设置数字格式化程序。我已经做了你想要的一切,除了货币位。您可以在文档中查找。

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];
[formatter setGroupingSeparator:groupingSeparator];
[formatter setGroupingSize:3];
[formatter setAlwaysShowsDecimalSeparator:NO];
[formatter setUsesGroupingSeparator:YES];

接下来,您要设置号码并返回格式化字符串。在您的例子中,我们将双精度数包装在 NSNumber 中。我是内联完成的,但您可以将其分为两个步骤:

NSString *formattedString = [formatter stringFromNumber:[NSNumber numberWithFloat:rev];

不要忘记清理!

[formatter release];

有关本地化的快速说明:

NSLocale 类提供了一些有关用户区域设置的有用信息。在第一步中,请注意我如何使用 NSLocale 来获取本地化的分组分隔符:(

NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];

有些国家/地区使用句号/句点,而其他国家/地区使用逗号。)我认为也有一种方法可以获取本地化的货币符号,但是我不是百分百确定,所以请检查文档。 (这取决于您想要做什么。)

You should definitely use NSNumberFormatter for this. The basic steps are:

  1. Allocate, initialize and configure your number formatter.
  2. Use the formatter to return a formatted string from a number. (It takes an NSNumber, so you'll need to convert your double or whatever primitive you have to NSNumber.)
  3. Clean up. (You know, memory management.)

This code sets up the number formatter. I've done everything that you want except the currency bit. You can look that up in the documentation.

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];
[formatter setGroupingSeparator:groupingSeparator];
[formatter setGroupingSize:3];
[formatter setAlwaysShowsDecimalSeparator:NO];
[formatter setUsesGroupingSeparator:YES];

Next, you want to set up your number and return a formatted string. In your case, we wrap a double in an NSNumber. I do it inline, but you can break it up into two steps:

NSString *formattedString = [formatter stringFromNumber:[NSNumber numberWithFloat:rev];

Don't forget to clean up!

[formatter release];

A quick note about localization:

The NSLocale class provides some useful info about the user's locale. In the first step, notice how I used NSLocale to get a localized grouping separator:

NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];

(Some countries use a full-stop/period, while others use a comma.) I think there's a way to get a localized currency symbol as well, but I'm not one hundred percent sure, so check the documentation. (It depends upon what your trying to do.)

再浓的妆也掩不了殇 2024-11-01 21:59:46

您将需要使用 NSNumberFormatter< /code>支持货币。

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSLog(@"%@", [currencyFormatter stringFromNumber:[NSNumber numberWithInt:10395209]]);
[currencyFormatter release];

印刷品:10,395,209.00 美元

You will need to use a NSNumberFormatter which supports currency.

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSLog(@"%@", [currencyFormatter stringFromNumber:[NSNumber numberWithInt:10395209]]);
[currencyFormatter release];

Prints: $10,395,209.00

久隐师 2024-11-01 21:59:46
[formatterCurrency setMaximumFractionDigits:0]

是在 NSNumberFormatterCurrencyStyle 格式化程序中截断小数数字和小数分隔符的唯一方法。

NSNumberFormatter *formatterCurrency;
formatterCurrency = [[NSNumberFormatter alloc] init];

formatterCurrency.numberStyle = NSNumberFormatterCurrencyStyle;
[formatterCurrency setMaximumFractionDigits:0];
[formatterCurrency stringFromNumber: @(12345.2324565)];

结果

12,345 $

[formatterCurrency setMaximumFractionDigits:0]

is only way to trancate decimal digits and decimal separator in a NSNumberFormatterCurrencyStyle formatter.

NSNumberFormatter *formatterCurrency;
formatterCurrency = [[NSNumberFormatter alloc] init];

formatterCurrency.numberStyle = NSNumberFormatterCurrencyStyle;
[formatterCurrency setMaximumFractionDigits:0];
[formatterCurrency stringFromNumber: @(12345.2324565)];

result

12,345 $

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