20

我可以使用 NSNUmberFormatter 将特定的 NSNumber 转换为货币。它的显示 $ 符号,用“,”分隔的数字等。
但我不想显示美分。
例如它显示 $537,335.32
我只想显示 $536,335
我正在编写以下代码:

NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSDictionary* tempDict = [self.displayDict objectForKey:kConservative];
conservativeHP.text = [currencyFormatter stringFromNumber:[tempDict objectForKey:kHomePrice]];

我应该如何创建 NSNumberFormatter 或做一些事情以使结果不会有“小数”

4

7 回答 7

53

使用 maximumFractionDigits 设置:

NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setMaximumFractionDigits:2];

您可以相应地调整该值。使用 2 会将结果限制为两位数,对于这个问题,应该使用 0 值。

于 2012-07-23T15:07:01.350 回答
16

这是swift 3中的解决方案

    let formatter = NumberFormatter()
    formatter.numberStyle = .currency
    //formatter.locale = NSLocale.current
    formatter.currencySymbol = "" //In case you do not want any currency symbols
    formatter.maximumFractionDigits = 0

设置maximumFractionDigits0不会显示任何小数或小数值。

于 2017-12-21T07:21:50.220 回答
1

斯威夫特 3:

currencyFormatter.maximumFractionDigits = 0
于 2017-07-14T20:47:08.207 回答
0

当你去显示它只是这样做

Mylabel.text = @"%.2f", mynumberwithcents;

这仅适用于mynumberwithcents变量是浮点数或双精度...

于 2014-07-20T22:26:35.230 回答
0
In case of using category:

    .h
        #import <Foundation/Foundation.h>

        @interface NSString (Price)

        + (NSString *)updateStringToPriceWithValue:(NSNumber *)value;

        @end
    .m
        #import "NSString+Price.h"
        @implementation NSString (Price)

        + (NSString *)updateStringToPriceWithValue:(NSNumber *)value
        {
            NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
            [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];

            NSInteger minimumFractionDigits = [formatter minimumFractionDigits];
            NSInteger maximumFractionDigits = [formatter maximumFractionDigits];

            NSString *formatted = [formatter stringFromNumber:value];
            NSLog(@"%@", formatted);

            [formatter setMinimumFractionDigits:minimumFractionDigits];
            [formatter setMaximumFractionDigits:maximumFractionDigits];



            return formatted;

        }
于 2017-09-24T21:39:59.863 回答
0
extension NumberFormatter {

static func makeDisplayPrice(byCode code: String, price: Int) -> String {
    let currencyFormatter = NumberFormatter.currencyPrice
    currencyFormatter.currencyCode = code
    guard let priceString = currencyFormatter.string(from: NSNumber(integerLiteral: price)) else { return "" }
    return priceString
}

static var currencyPrice: NumberFormatter {
    let currencyFormatter = NumberFormatter()
    currencyFormatter.usesGroupingSeparator = false
    currencyFormatter.numberStyle = .currency
    currencyFormatter.locale = Locale.current
    currencyFormatter.maximumFractionDigits = 0
    return currencyFormatter
}

}

let displayPrice = NumberFormatter.makeDisplayPrice(byCode: currency, price: price)
于 2019-06-05T13:27:04.677 回答
-3

在将数字添加到格式化程序之前,将其更改为 int 以避免小数,如下所示:

float myValue = 543.32;

NSNumber myNumber = [NSNumber numberWithInt: (int)myValue)];

您将摆脱小数部分!

于 2012-03-08T09:32:10.637 回答