package util import ( "fmt" "math" ) // Round a given number to the number of places indicated. roundOn indicates what fraction to round up on (ex: .5) func Round(val float64, roundOn float64, places int) (newVal float64) { var round float64 pow := math.Pow(10, float64(places)) digit := pow * val _, div := math.Modf(digit) if div >= roundOn { round = math.Ceil(digit) } else { round = math.Floor(digit) } newVal = round / pow return } func WithinVariance(x, y, variance int) (bool, error) { v := math.Abs(float64(variance)) d := math.Abs(float64(x) - float64(y)) if d >= v { return false, fmt.Errorf("Variance of %v larger than %v", d, variance) } return true, nil }