FormatMoney

This commit is contained in:
jiangyong27 2024-01-05 21:40:47 +08:00
parent 74eb81da20
commit 64fe0dfafb
2 changed files with 51 additions and 25 deletions

32
file.go
View File

@ -1,6 +1,9 @@
package goutil
import "io/ioutil"
import (
"io/ioutil"
"os"
)
func FileList(dirPath string) ([]string, error) {
lis, err := ioutil.ReadDir(dirPath)
@ -17,3 +20,30 @@ func FileList(dirPath string) ([]string, error) {
}
return fileList, nil
}
// HasDir 判断文件夹是否存在
func ExistDir(path string) (bool, error) {
_, _err := os.Stat(path)
if _err == nil {
return true, nil
}
if os.IsNotExist(_err) {
return false, nil
}
return false, _err
}
// CreateDir 创建文件夹
func CreateDir(path string) error {
_exist, _err := ExistDir(path)
if _err != nil {
return _err
}
if !_exist {
err := os.Mkdir(path, os.ModePerm)
if err != nil {
return err
}
}
return nil
}

44
util.go
View File

@ -9,18 +9,27 @@ import (
"strings"
)
// format bytes number friendly
func BytesToTips(bytes uint64) string {
switch {
case bytes < 1024:
return fmt.Sprintf("%dB", bytes)
case bytes < 1024*1024:
return fmt.Sprintf("%.2fK", float64(bytes)/1024)
case bytes < 1024*1024*1024:
return fmt.Sprintf("%.2fM", float64(bytes)/1024/1024)
default:
return fmt.Sprintf("%.2fG", float64(bytes)/1024/1024/1024)
// FormatMoney 格式化商品价格
func FormatMoney(number int64) string {
num1 := float64(number) / 100
num2 := float64(number / 100)
if num1 != num2 {
return fmt.Sprintf("%.2f", num1)
}
return strconv.FormatInt(int64(num1), 10)
}
func FormatBytes(bytes int64) string {
const unit = 1024
if bytes < unit {
return strconv.FormatInt(bytes, 10) + " B"
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.2f%cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}
func If[T any](condition bool, trueVal, falseVal T) T {
@ -79,19 +88,6 @@ func CopyStruct(dst interface{}, src interface{}) {
}
func FormatBytes(bytes int64) string {
const unit = 1024
if bytes < unit {
return strconv.FormatInt(bytes, 10) + " B"
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.2f%cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}
func HtmlStrip(src string) string {
//将HTML标签全转换成小写
re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")