从货币中删除小数位数?

时间:2015-06-19 20:46:40

标签: ios swift nsdate

我有以下示例:

3

我希望货币输出有2个摘要。如果货币摘要全部为零,我希望它们不会显示。因此3,00应显示为:{{1}}。应使用两个摘要显示所有其他值。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

你必须将numberStyle设置为DecimalStyle才能设置minimumFractionDigits属性,具体取决于double是否为even:

let birthdate = try currentHealthStore.dateOfBirthWithError()

修改

extension Double {
    var formatted: String {
        let numberFormater = NSNumberFormatter()
        numberFormater.locale = NSLocale.currentLocale()
        numberFormater.numberStyle = NSNumberFormatterStyle.DecimalStyle
        numberFormater.maximumFractionDigits = 2
        numberFormater.minimumFractionDigits = self - Double(Int(self)) == 0 ? 0 : 2
        return numberFormater.stringFromNumber(self) ?? ""
    }
}


3.0.formatted    // "3"
3.12.formatted   // "3.12"
3.20.formatted   // "3.20"

var price = 3.20
println("price: \(price.formatted)")

答案 1 :(得分:0)

您可以只使用NSString init(格式:参数:)

而不是使用NSNumberFormatter

var ShowRoute = Ember.Route.extend({ model: function(params) { // Load the model and return it. // This will only fire if the model isn't passed. }, afterModel: function(model, transition) { // Load the rest of the data based on the model and return it. // This fires every time the route re-loads (wether passed in or via model method). } });

我对swift并不擅长,但这应该有用。

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265