没有零的Swift货币风格

Vas*_*ily 3 number-formatting swift

我正在使用CurrencyStyle数字格式化程序.它产生的输出类似于"$ 521.00".是不是在不编写自定义数字格式化程序的情况下删除尾随.XX的简单方法?

我希望输出像"$ 1,521"没有尾随零.

var asCurrency: String {
        let formatter = NSNumberFormatter()
        formatter.numberStyle = .CurrencyStyle
        formatter.locale = NSLocale(localeIdentifier: "en_US")
        if let formattedPrice = formatter.stringFromNumber(self) {
            return formattedPrice
        } 
Run Code Online (Sandbox Code Playgroud)

Mar*_*n R 5

如果您想打印一般没有小数位的金额,那么您可以设置

formatter.maximumFractionDigits = 0
Run Code Online (Sandbox Code Playgroud)

因为1521你将获得字符串$1,521.非整数值是四舍五入的,因此1234.56为此将生成字符串$1,235.舍入行为也可以使用roundingMode 属性进行控制.