swift——数字处理,数字太长用千、万展示

2023-07-31  本文已影响0人  不洗澡的鱼
/**
    Int(floor(thousand)):向下取整
    round(thousand * 10) / 10:四舍五入保留一位小数
*/
func numberPintUnit(from: Double) -> String{
        let number = from
        let billion = number / 100_000_000
        let kmillion = number / 10_000_000
        let million = number / 1_000_000
        let wan = number / 10000
        let thousand = number / 1000
        
        if billion >= 1.0 {
            return "\(Int(floor(billion)))亿+"//\(round(billion * 10) / 10)
        }else if kmillion >= 1.0 {
            return "\(Int(floor(kmillion)))千万+"
        }
        else if million >= 1.0 {
            return "\(Int(floor(million)))百万+"//\(round(million * 10) / 10)
        } else if wan >= 1.0{
            return "\(Int(floor(wan)))万+"//\(round(thousand * 10) / 10)
        }
        else if thousand >= 1.0 {
            return "\(Int(floor(thousand)))千+"//\(round(thousand * 10) / 10)
        } else {
            return "\(Int(number))"
        }
    }

上一篇 下一篇

猜你喜欢

热点阅读