extension Float {
/** 战斗力值转换 */
var combatValues: String {
if (self / 10000) > 1 {
let temp = self / 10000
if fmodf(Float(temp), 1) == 0 {
let str = String.init(format: "%.0f", temp)
return str + "万"
}else if fmodf(Float(temp) * 10, 1) == 0 {
let str = String.init(format: "%.1f", temp)
return str + "万"
}else{
let str = String.init(format: "%.2f", temp)
let decimal = str.componentsSeparatedByString(".")
let arrayString = decimal[1]
if arrayString == "00" {
return decimal[0] + "万"
}
return str + "万"
}
}
if (self / 100000000) > 1 {
let temp = self / 100000000
if fmodf(Float(temp), 1) == 0 {
let str = String.init(format: "%.0f", temp)
return str + "亿"
}else if fmodf(Float(temp) * 10, 1) == 0 {
let str = String.init(format: "%.1f", temp)
return str + "亿"
}else{
let str = String.init(format: "%.2f", temp)
let decimal = str.componentsSeparatedByString(".")
let arrayString = decimal[1]
if arrayString == "00" {
return decimal[0] + "亿"
}
return str + "亿"
}
}
let decimal = String.init(self).componentsSeparatedByString(".")
let arrayString = decimal[1]
if arrayString == "0" {
return decimal[0]
}
return String.init(self)
}
}
字符串扩展
extension String {
func attributedString(font:CGFloat)->NSMutableAttributedString{
let attributedS = NSMutableAttributedString(string: self)
if self.containsString("万") || self.containsString("亿") {
let normalAttributes = [NSFontAttributeName:UIFont.systemFontOfSize(font)]
attributedS.addAttributes(normalAttributes, range: NSMakeRange(self.characters.count - 1,1))
}
return attributedS
}
}
调用:label.attributedText = Float(number).combatValues
网友评论