github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/common/math.go (about)

     1  package common
     2  
     3  import (
     4  	"crypto/md5"
     5  	"encoding/hex"
     6  	"fmt"
     7  	"io"
     8  	"math"
     9  )
    10  
    11  func MD5Encrypt(data string) string {
    12  	m := md5.New()
    13  	m.Write([]byte(data))
    14  	return hex.EncodeToString(m.Sum(nil))
    15  }
    16  
    17  func MD5Encrypt2(data string) string {
    18  	m := md5.New()
    19  	io.WriteString(m, data)
    20  	return fmt.Sprintf("%X", m.Sum(nil))
    21  }
    22  
    23  // 四舍五入,保留n位小数
    24  func Round(f float64, n int) float64 {
    25  	pow10N := math.Pow10(n)
    26  	return math.Trunc((f+0.5/pow10N)*pow10N) / pow10N
    27  }