美文网首页
Swift 把 1234656转化成 1.23万 或1.23亿

Swift 把 1234656转化成 1.23万 或1.23亿

作者: ShineYangGod | 来源:发表于2019-12-12 17:00 被阅读0次
    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

    相关文章

      网友评论

          本文标题:Swift 把 1234656转化成 1.23万 或1.23亿

          本文链接:https://www.haomeiwen.com/subject/dvptnctx.html