3

在我的应用程序中,我正在处理大数字(货币),我正在使用 nsnumberformatter 对其进行格式化。但是目前,小数位数是根据语言环境设置的。由于涉及到大数字,我不想显示小数位任何货币。如何在 nsnumberformatter 货币样式中将小数位设置为零?

使用的代码

NSDecimalNumber *someAmount = [NSDecimalNumber decimalNumberWithString:unformattedString];
    NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
    [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [currencyFormatter setLocale:locale];
    NSString *str=[NSString stringWithFormat:@"%.0f",someAmount];
    //NSString *str=[currencyFormatter stringFromNumber:someAmount];
4

1 回答 1

8

使用下面的代码从数字格式化程序中删除小数位:

[currencyFormatter setMaximumFractionDigits:0];

使用上面的代码的完整代码:

NSDecimalNumber *someAmount = [NSDecimalNumber decimalNumberWithString:unformattedString];
NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setLocale:locale];
[currencyFormatter setMaximumFractionDigits:0];
NSString *str=[currencyFormatter stringFromNumber:someAmount];
于 2011-11-11T12:53:20.880 回答