我当前的代码是
NSNumberFormatter *f = [[[NSNumberFormatter alloc] init] autorelease];
[f setNumberStyle:NSNumberFormatterCurrencyStyle];
[f setCurrencySymbol:NSLocalizedString(@"CURRENCY", @"Get Currency")];
NSString * stringCurrecy = [f stringFromNumber:(-70.00)];我正在使用NSLog检查字符串currency,它正在打印"($ 70.00)“。我改了"(",")“符号。我如何实现这一点:
( $ 70.00 ) -> - $70.00 or $ -70.00发布于 2011-02-11 15:53:24
您必须设置负数格式。添加下面这行:
[f setNegativeFormat:@"-¤#,##0.00"];但是,您可能只想使用[f setLocale:]设置区域设置,而不是自己设置每个格式选项。
(下一次发布可以编译的代码。)
发布于 2012-09-05 02:56:41
我有一个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发布于 2014-07-23 10:10: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。
https://stackoverflow.com/questions/4966501
复制相似问题