NSNumberFormatter 货币负数

发布于 2024-10-17 07:27:45 字数 437 浏览 3 评论 0原文

我当前的代码是

NSNumberFormatter *f = [[[NSNumberFormatter alloc] init] autorelease];
[f setNumberStyle:NSNumberFormatterCurrencyStyle];
[f setCurrencySymbol:NSLocalizedString(@"CURRENCY", @"Get Currency")];
NSString * stringCurrecy = [f stringFromNumber:(-70.00)];

使用 NSLog 来检查字符串货币并打印“($ 70.00)”。 我改变了“(”,“)”符号。我怎样才能做到这一点:

( $ 70.00 ) -> - $70.00 or $ -70.00

My current code is

NSNumberFormatter *f = [[[NSNumberFormatter alloc] init] autorelease];
[f setNumberStyle:NSNumberFormatterCurrencyStyle];
[f setCurrencySymbol:NSLocalizedString(@"CURRENCY", @"Get Currency")];
NSString * stringCurrecy = [f stringFromNumber:(-70.00)];

I'm using NSLog to check the string currency and it's printing "($ 70.00)".
I changed "(", ")" symbol. How can I achieve this:

( $ 70.00 ) -> - $70.00 or $ -70.00

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

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

发布评论

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

评论(3

萌面超妹 2024-10-24 07:27:45

您必须设置负数格式。
添加此行:

[f setNegativeFormat:@"-¤#,##0.00"];

但也许您只想使用 [f setLocale:] 设置区域设置,而不是自己设置每个格式选项。

(下次发布可以编译的代码。)

You have to set the negative format.
Add this line:

[f setNegativeFormat:@"-¤#,##0.00"];

but maybe you just want to set the locale with [f setLocale:] instead of setting every format option on your own.

(and next time post code that does compile.)

浮生未歇 2024-10-24 07:27:45

我有一个 NSNumber 类别可以执行以下操作:

@implementation NSNumber (Formatter)

- (NSString *)currencyStringValue
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.locale = [NSLocale currentLocale];
    formatter.numberStyle = NSNumberFormatterCurrencyStyle;
    formatter.negativePrefix = [NSString stringWithFormat:@"- %@", formatter.currencySymbol];
    formatter.negativeSuffix = @"";

    return [formatter stringFromNumber:self];
}

@end

I have a NSNumber category that does this:

@implementation NSNumber (Formatter)

- (NSString *)currencyStringValue
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.locale = [NSLocale currentLocale];
    formatter.numberStyle = NSNumberFormatterCurrencyStyle;
    formatter.negativePrefix = [NSString stringWithFormat:@"- %@", formatter.currencySymbol];
    formatter.negativeSuffix = @"";

    return [formatter stringFromNumber:self];
}

@end
请你别敷衍 2024-10-24 07:27:45

将 negativeFormat 设置为“-”几乎对我有用,但它会丢失货币符号。至少对于 en_US 语言环境,这符合我的需求:

NSLocale *US = [NSLocale localeWithLocaleIdentifier:@"en_US"];
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setCurrencyCode:currencyCode];
[currencyFormatter setLocale:priceLocale];

//manually add - prefix to positive format...
NSString *negFormat = [NSString stringWithFormat:@"-%@",currencyFormatter.positiveFormat];
[currencyFormatter setNegativeFormat:negFormat];

我不确定这是否适合所有语言环境,但它适用于 en_US。

Setting negativeFormat to "-" almost worked for me, but it was dropping off the currency symbol. At least for en_US locale, this fits my needs:

NSLocale *US = [NSLocale localeWithLocaleIdentifier:@"en_US"];
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setCurrencyCode:currencyCode];
[currencyFormatter setLocale:priceLocale];

//manually add - prefix to positive format...
NSString *negFormat = [NSString stringWithFormat:@"-%@",currencyFormatter.positiveFormat];
[currencyFormatter setNegativeFormat:negFormat];

I'm not sure whether this is appropriate for all locales, but it works for en_US.

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