MathUtils.go 659 Bytes
Newer Older
吴贤德's avatar
吴贤德 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
package Math

import (
	"fmt"
	"math"
	"math/rand"
	"strconv"
	"time"
)

func Round(f float64, n int) float64 {
	n10 := math.Pow10(n)
	return math.Trunc((f+0.5/n10)*n10) / n10
}
func Round2(f float64, n int) float64 {
	floatStr := fmt.Sprintf("%."+strconv.Itoa(n)+"f", f)
	inst, _ := strconv.ParseFloat(floatStr, 64)
	return inst
}

// 函 数:生成随机数
// 概 要:
// 参 数:
//      min: 最小值
//      max: 最大值
// 返回值:
//      int64: 生成的随机数
func RandInt64(min, max int64) int64 {
	rand.Seed(time.Now().UnixNano())
	if min >= max || min == 0 || max == 0 {
		return max
	}
	return rand.Int63n(max-min) + min
}