如何将货币格式化为货币 - Swift 3 [英] How to format a Double into Currency - Swift 3

查看:227
本文介绍了如何将货币格式化为货币 - Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swift编程的新手,我已经在Xcode 8.2中创建了一个简单的小费计算器应用程序,我在下面的 IBAction 中设置了计算。但是当我真正运行我的应用程序,并输入一个金额来计算(如23.45),它会出现超过2个小数位。在这种情况下,如何将它格式化为 .currency

pre $ IBAction func calculateButtonTapped(_ sender:Any){

var tipPercentage:Double {

如果tipAmountSegmentedControl.selectedSegmentIndex == 0 {
return 0.05
} else if tipAmountSegmentedControl.selectedSegmentIndex == 1 {
return 0.10
} else {
return 0.2
}
}

let billAmount:Double ? = Double(userInputTextField.text!)

if billAmount = billAmount {
let tipAmount = billAmount * tipPercentage
let totalBillAmount = billAmount + tipAmount

tipAmountLabel.text =Tip Amount:$ \(tipAmount)
totalBillAmountLabel.text =Total Bill Amount:$ \(totalBillAmount)
}
}


解决方案您可以使用此字符串初始值设定项,如果您想强制货币为$:

 字符串(格式:Tip Amount:$%。02f,tipAmount)

如果您希望它完全依赖于设备的区域设置,您应该使用 NumberFormatter 。这将考虑货币的小数位数,以及正确定位货币符号。例如。 double值2.4会返回es_ES语言环境的2,40€和jp_JP语言环境的¥2。

 让formatter = NumberFormatter()
formatter.locale = Locale.current //如果你想强制一个特定的语言环境,将其改为另一个语言环境,否则这是多余的,因为当前的语言环境是默认的
格式化程序.nu​​mberStyle = .currency
if formattedTipAmount = formatter.string(from:tipAmount as NSNumber){
tipAmountLabel.text =提示金额:\(formattedTipAmount)
}


I'm new to Swift programming and I've been creating a simple tip calculator app in Xcode 8.2, I have my calculations set up within my IBAction below. But when I actually run my app and input an amount to calculate (such as 23.45), it comes up with more than 2 decimal places. How do I format it to .currency in this case?

@IBAction func calculateButtonTapped(_ sender: Any) {

    var tipPercentage: Double {

        if tipAmountSegmentedControl.selectedSegmentIndex == 0 {
            return 0.05
        } else if tipAmountSegmentedControl.selectedSegmentIndex == 1 {
            return 0.10
        } else {
            return 0.2
        }
    }

    let billAmount: Double? = Double(userInputTextField.text!)

    if let billAmount = billAmount {
        let tipAmount = billAmount * tipPercentage
        let totalBillAmount = billAmount + tipAmount

        tipAmountLabel.text = "Tip Amount: $\(tipAmount)"
        totalBillAmountLabel.text = "Total Bill Amount: $\(totalBillAmount)"
    }
}

解决方案

You can use this string initializer if you want to force the currency to $:

String(format: "Tip Amount: $%.02f", tipAmount)

If you want it to be fully dependent on the locale settings of the device, you should use a NumberFormatter. This will take into account the number of decimal places for the currency as well as positioning the currency symbol correctly. E.g. the double value 2.4 will return "2,40 €" for the es_ES locale and "¥ 2" for the jp_JP locale.

let formatter = NumberFormatter()
formatter.locale = Locale.current // Change this to another locale if you want to force a specific locale, otherwise this is redundant as the current locale is the default already
formatter.numberStyle = .currency
if let formattedTipAmount = formatter.string(from: tipAmount as NSNumber) {
    tipAmountLabel.text = "Tip Amount: \(formattedTipAmount)"
}

这篇关于如何将货币格式化为货币 - Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆