iOS - 使用NSNumberFormatter显示没有美分的货币

时间:2012-03-08 09:25:52

标签: ios nsnumberformatter

我可以使用NSNUmberFormatter将特定的NSNumber转换为货币。它的showign $符号,用“,”等分隔的数字 但我不想显示美分。
例如它显示$ 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或执行某些操作以使结果不具有“小数”

7 个答案:

答案 0 :(得分:41)

使用maximumFractionDigits设置:

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

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

答案 1 :(得分:4)

以下是 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

maximumFractionDigits设置为0不会显示任何小数或小数值。

答案 2 :(得分:0)

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

float myValue = 543.32;

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

你将摆脱小数部分!

答案 3 :(得分:0)

当你去展示它时,只需这样做

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

仅当mynumberwithcents变量是Float或Double ...

时才有效

答案 4 :(得分: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;

        }

答案 5 :(得分: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)

答案 6 :(得分:-1)

斯威夫特3:

currencyFormatter.maximumFractionDigits = 0