项目中有个需求是去掉小数点后面的0并取有效值,查找资料后修改完善如下
//去掉小数点后面的0
func quXiaoShuDianHouDeLing(numberString:String) -> String{
var outNumber = numberString
var i = 1
if numberString.contains("."){
while i < numberString.count{
if outNumber.hasSuffix("0") {
outNumber.remove(at: outNumber.index(before: outNumber.endIndex))
i = i + 1
} else {
break
}
}
if outNumber.hasSuffix("."){
outNumber.remove(at: outNumber.index(before: outNumber.endIndex))
}
return outNumber
} else {
return numberString
}
}
调用的话这样:
priceLabel.text = String(format: "折扣价:%@万元", quXiaoShuDianHouDeLing(numberString: model.amount))
后台返回是这种的: