github.com/wtfutil/wtf@v0.43.0/wtf/numbers.go (about)

     1  package wtf
     2  
     3  import "math"
     4  
     5  // Round rounds a float to an integer
     6  func Round(num float64) int {
     7  	return int(num + math.Copysign(0.5, num))
     8  }
     9  
    10  // TruncateFloat64 truncates the decimal places of a float64 to the specified precision
    11  func TruncateFloat64(num float64, precision int) float64 {
    12  	output := math.Pow(10, float64(precision))
    13  	return float64(Round(num*output)) / output
    14  }