27 lines
457 B
Go
27 lines
457 B
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"math/rand"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func Round(f float64, n int) float64 {
|
|
pow10 := math.Pow10(n)
|
|
return math.Trunc((f+0.5/pow10)*pow10) / pow10
|
|
}
|
|
|
|
func GenValidateCode(width int) string {
|
|
numeric := [9]byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
|
r := len(numeric)
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
var sb strings.Builder
|
|
for i := 0; i < width; i++ {
|
|
fmt.Fprintf(&sb, "%d", numeric[rand.Intn(r)])
|
|
}
|
|
return sb.String()
|
|
}
|