golang go语言中 对文件大小字节单位的换算 EB TB GB MB KB B 保留小数

// 字节的单位转换 保留两位小数
func formatFileSize(fileSize int64) (size string) {
   if fileSize < 1024 {
      //return strconv.FormatInt(fileSize, 10) + "B"
      return fmt.Sprintf("%.2fB", float64(fileSize)/float64(1))
   } else if fileSize < (1024 * 1024) {
      return fmt.Sprintf("%.2fKB", float64(fileSize)/float64(1024))
   } else if fileSize < (1024 * 1024 * 1024) {
      return fmt.Sprintf("%.2fMB", float64(fileSize)/float64(1024*1024))
   } else if fileSize < (1024 * 1024 * 1024 * 1024) {
      return fmt.Sprintf("%.2fGB", float64(fileSize)/float64(1024*1024*1024))
   } else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
      return fmt.Sprintf("%.2fTB", float64(fileSize)/float64(1024*1024*1024*1024))
   } else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
      return fmt.Sprintf("%.2fEB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
   }
}
发布了6 篇原创文章 · 获赞 2 · 访问量 5713

猜你喜欢

转载自blog.csdn.net/gaoluhua/article/details/104591055
今日推荐