【发布时间】:2017-05-21 16:34:16
【问题描述】:
我的 NSDecimalNumber 中有一个扩展名
extension NSDecimalNumber {
func format(f: String) -> String {
return String(format: "%\(f)f", self)
}
}
这应该允许我执行以下操作:
var price = 34.2499999
price.format(f: ".2") // 35.25
相反,我在UICollectionViewCell 中得到 0.00:
func configureCell(_ item: Item) {
self. item = item
nameLabel.adjustsFontSizeToFitWidth = true
priceLabel.adjustsFontSizeToFitWidth = true
nameLabel.text = self. item.name.capitalized
priceLabel.text = "£\(self. item.price!.format(f: ".2"))"
}
我想用显示两位小数而不是随机数的实际价格来格式化它。所有价格均从数据库中检索,准确无误,仅保留 2 位小数。
由于某种原因,当我从扩展名中删除格式时,我得到更多小数点,而我在数据库中只有 2 个。为什么会发生这种情况以及如何解决?
【问题讨论】:
-
您定义了
NSDecimalNumber的扩展,它不会应用于Double。
标签: ios swift uicollectionview swift3 number-formatting