mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-28 02:19:04 +08:00
模块化上传驱动,使用泛型优化工具库降低冗余
This commit is contained in:
@@ -7,40 +7,67 @@ package format
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Round2String 四舍五入保留小数,默认2位
|
||||
func Round2String(value float64, args ...interface{}) (v string) {
|
||||
func Round2String(value float64, args ...interface{}) string {
|
||||
var places = 2
|
||||
if len(args) > 0 {
|
||||
places = gconv.Int(args[0])
|
||||
}
|
||||
|
||||
cDig := strconv.Itoa(places)
|
||||
val := fmt.Sprintf("%0."+cDig+"f", value)
|
||||
return val
|
||||
return fmt.Sprintf("%0."+strconv.Itoa(places)+"f", value)
|
||||
}
|
||||
|
||||
// Round2Float64 四舍五入保留小数,默认2位
|
||||
func Round2Float64(value float64, args ...interface{}) (v float64) {
|
||||
func Round2Float64(value float64, args ...interface{}) float64 {
|
||||
return gconv.Float64(Round2String(value, args...))
|
||||
}
|
||||
|
||||
// FileSize 字节的单位转换 保留两位小数
|
||||
func FileSize(fileSize int64) (size string) {
|
||||
if fileSize < 1024 {
|
||||
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 {
|
||||
return fmt.Sprintf("%.2fEB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
|
||||
func FileSize(data int64) string {
|
||||
var factor float64 = 1024
|
||||
res := float64(data)
|
||||
for _, unit := range []string{"", "K", "M", "G", "T", "P"} {
|
||||
if res < factor {
|
||||
return fmt.Sprintf("%.2f%sB", res, unit)
|
||||
}
|
||||
res /= factor
|
||||
}
|
||||
return fmt.Sprintf("%.2f%sB", res, "P")
|
||||
}
|
||||
|
||||
// AgoTime 多久以前
|
||||
func AgoTime(gt *gtime.Time) string {
|
||||
if gt == nil {
|
||||
return ""
|
||||
}
|
||||
n := gtime.Now().Timestamp()
|
||||
t := gt.Timestamp()
|
||||
|
||||
var ys int64 = 31536000
|
||||
var ds int64 = 86400
|
||||
var hs int64 = 3600
|
||||
var ms int64 = 60
|
||||
var ss int64 = 1
|
||||
var rs string
|
||||
|
||||
d := n - t
|
||||
switch {
|
||||
case d > ys:
|
||||
rs = fmt.Sprintf("%d年前", int(d/ys))
|
||||
case d > ds:
|
||||
rs = fmt.Sprintf("%d天前", int(d/ds))
|
||||
case d > hs:
|
||||
rs = fmt.Sprintf("%d小时前", int(d/hs))
|
||||
case d > ms:
|
||||
rs = fmt.Sprintf("%d分钟前", int(d/ms))
|
||||
case d > ss:
|
||||
rs = fmt.Sprintf("%d秒前", int(d/ss))
|
||||
default:
|
||||
rs = "刚刚"
|
||||
}
|
||||
return rs
|
||||
}
|
||||
|
Reference in New Issue
Block a user