亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Swift中與NSNumberFormatter爭奪貨幣

在Swift中與NSNumberFormatter爭奪貨幣

嚕嚕噠 2019-11-12 14:48:47
我正在創建一個預算應用程序,允許用戶輸入他們的預算以及交易。我需要允許用戶從單獨的文本字段中輸入便士和英鎊,并且它們需要與貨幣符號一起設置格式。我目前可以正常使用,但希望將其本地化,因為目前僅適用于GBP。我一直在努力地將NSNumberFormatter示例從Objective C轉移到Swift。我的第一個問題是我需要為輸入字段設置占位符,使其特定于用戶位置。例如。磅,便士,美元和美分等...第二個問題是,需要格式化在每個文本字段(例如10216和32)中輸入的值,并且需要添加特定于用戶位置的貨幣符號。因此它將變成10,216.32英鎊或10,216.32美元,等等。另外,我需要在計算中使用格式化數字的結果。那么,如何在不遇到貨幣符號問題的情況下做到這一點呢?任何幫助將非常感激。
查看完整描述

3 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

這是一個有關如何在Swift 3上使用它的示例。(編輯:在Swift 4中也可以使用)


let price = 123.436 as NSNumber


let formatter = NumberFormatter()

formatter.numberStyle = .currency

// formatter.locale = NSLocale.currentLocale() // This is the default

// In Swift 4, this ^ has been renamed to simply NSLocale.current

formatter.string(from: price) // "$123.44"


formatter.locale = Locale(identifier: "es_CL")

formatter.string(from: price) // $123"


formatter.locale = Locale(identifier: "es_ES")

formatter.string(from: price) // "123,44 €"

這是關于如何在Swift 2上使用它的舊示例。


let price = 123.436


let formatter = NSNumberFormatter()

formatter.numberStyle = .CurrencyStyle

// formatter.locale = NSLocale.currentLocale() // This is the default

formatter.stringFromNumber(price) // "$123.44"


formatter.locale = NSLocale(localeIdentifier: "es_CL")

formatter.stringFromNumber(price) // $123"


formatter.locale = NSLocale(localeIdentifier: "es_ES")

formatter.stringFromNumber(price) // "123,44 €"


查看完整回答
反對 回復 2019-11-12
?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

如果您正在尋找一種可以為您提供解決方案的解決方案:


“ 5” =“ $ 5”

“ 5.0” =“ $ 5”

“ 5.00” =“ $ 5”

“ 5.5” =“ $ 5.50”

“ 5.50” =“ $ 5.50”

“ 5.55” =“ $ 5.55”

“ 5.234234” =“ 5.23”

請使用以下內容:


func cleanDollars(_ value: String?) -> String {

? ? guard value != nil else { return "$0.00" }

? ? let doubleValue = Double(value!) ?? 0.0

? ? let formatter = NumberFormatter()

? ? formatter.currencyCode = "USD"

? ? formatter.currencySymbol = "$"

? ? formatter.minimumFractionDigits = (value!.contains(".00")) ? 0 : 2

? ? formatter.maximumFractionDigits = 2

? ? formatter.numberStyle = .currencyAccounting

? ? return formatter.string(from: NSNumber(value: doubleValue)) ?? "$\(doubleValue)"

}


查看完整回答
反對 回復 2019-11-12
?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

我還實現了@Ni?oScript提供的解決方案作為擴展:


延期


// Create a string with currency formatting based on the device locale

//

extension Float {

    var asLocaleCurrency:String {

        var formatter = NSNumberFormatter()

        formatter.numberStyle = .CurrencyStyle

        formatter.locale = NSLocale.currentLocale()

        return formatter.stringFromNumber(self)!

    }

}

用法:


let amount = 100.07

let amountString = amount.asLocaleCurrency

print(amount.asLocaleCurrency())

// prints: "$100.07"

迅捷3


    extension Float {

    var asLocaleCurrency:String {

        var formatter = NumberFormatter()

        formatter.numberStyle = .currency

        formatter.locale = Locale.current

        return formatter.string(from: self)!

    }

}


查看完整回答
反對 回復 2019-11-12
  • 3 回答
  • 0 關注
  • 636 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號