27 lines
452 B
Go
27 lines
452 B
Go
|
package util
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"math"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
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
|
||
|
}
|