swift基础--获取描述文件大小

func getSizeDescription(size:String)->String{

    if(size.isEmpty){

        return "0B"

    }

    let s = Double(size)!

    let t = s / pow(1024,4)

    if(t > 1){

        return String(format: "%0.1f",t)+ "T"

    }

    let g = s / pow(1024,3)


    if(g > 1){

        return String(format: "%0.1f",g)+ "G"

    }


    let m = s / pow(1024,2)

    if(m > 1){

        return String(format: "%0.1f",m)+ "M"

    }

    let k = s / pow(1024,1)

    if(k > 1){

        return String(format: "%.1f",k)+ "k"

    }


    return size + "B"


}

你可能感兴趣的:(swift基础--获取描述文件大小)