33 lines
591 B
Go
33 lines
591 B
Go
package util
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"strconv"
|
|
"regexp"
|
|
)
|
|
|
|
func StringToFloat64(str string) float64 {
|
|
n, _ := strconv.ParseFloat(str, 64)
|
|
return n
|
|
}
|
|
|
|
func Float64ToString(num float64) string {
|
|
|
|
return fmt.Sprintf("%v", toFixed(num, 2))
|
|
}
|
|
|
|
func round(num float64) int {
|
|
return int(num + math.Copysign(0.5, num))
|
|
}
|
|
|
|
func toFixed(num float64, precision int) float64 {
|
|
output := math.Pow(10, float64(precision))
|
|
return float64(round(num*output)) / output
|
|
}
|
|
|
|
func ExtractInBracket(str string) string {
|
|
regex := regexp.MustCompile(`\[(.*?)\]`)
|
|
return regex.FindStringSubmatch(str)[1]
|
|
}
|