Swift4.0 科学记数法的问题
1.取消科学记数法
class func cancleScientific(num:String) ->String{
letformatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = .decimal
letnumber = formatter.number(from: num)
ifletn = number{
returnString(format:"%.8f", n.doubleValue)
}else{
return"0"
}
}
2.去除无效的0
// 去除无效
classfuncdeleteInvalidNum(num:String) ->String{
varoutNumber = num
vari =1
ifnum.contains("."){
whilei<num.count{
ifoutNumber.hasSuffix("0"){
outNumber.remove(at: outNumber.index(before: outNumber.endIndex))
i = i+1
}else{
break
}
}
ifoutNumber.hasSuffix("."){
outNumber.remove(at: outNumber.index(before: outNumber.endIndex))
}
returnoutNumber
}else{
returnnum
}
}
3.使用
上述两个方法我放在了common.swift文件中
str =common.cancleScientific(num:"\(model.amount??0)")
amount =common.deleteInvalidNum(num: amountstr)