2017-10-24 07:09:17 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strconv"
|
2017-10-24 09:23:56 +00:00
|
|
|
"regexp"
|
2017-10-24 07:09:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-10-24 09:23:56 +00:00
|
|
|
|
|
|
|
func ExtractInBracket(str string) string {
|
|
|
|
regex := regexp.MustCompile(`\[(.*?)\]`)
|
|
|
|
return regex.FindStringSubmatch(str)[1]
|
|
|
|
}
|