2017-05-30 59 views
0

我試圖採取十進制我存儲在CoreData並通過斯威夫特3.這裏的貨幣格式化運行它就是我想要使用:如何使用的CurrencyFormatter帶小數點

var currencyFormatter = NumberFormatter() 
currencyFormatter.usesGroupingSeparator = true 
currencyFormatter.numberStyle = NumberFormatter.Style.currency 
// localize to your grouping and decimal separator 
currencyFormatter.locale = NSLocale.current 
var priceString = currencyFormatter.stringFromNumber(NSNumber(totalAmount)) 

其中totalAmount是我用於CoreData的小數。

但是。想我的十進制轉換爲NSNumber的()時,我得到這個錯誤

Argument labels '(_:)' do not match any available overloads

+0

使用'字符串(來源:)'或'字符串(:)' – Sulthan

回答

1

stringFromNumber得到了重新命名爲string(from:),例如

var priceString = currencyFormatter.string(from: NSNumber(totalAmount)) 

,但你不必轉換爲NSNumber

var priceString = currencyFormatter.string(for: totalAmount) 
0

你可以有這樣的事情:

class YourClass: UIViewController { 
    static let priceFormatter: NumberFormatter = { 
    let formatter = NumberFormatter() 

    formatter.formatterBehavior = .behavior10_4 
    formatter.numberStyle = .currency 

    return formatter 
    }() 
} 

用法:

yourLabel.text = YourClass.priceFormatter.string(from: totalAmount) 
相關問題