当前位置: 代码迷 >> 综合 >> golang go语言中 对文件大小字节单位的换算 EB TB GB MB KB B 保留小数
  详细解决方案

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

热度:0   发布时间:2024-01-26 20:42:14.0
// 字节的单位转换 保留两位小数
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))}
}