mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-09 15:12:26 +08:00
feat(mathutil): add DIv, FloorToFloat, FloorToString, CeilToFloat, CeilToString (#199)
This commit is contained in:
@@ -66,7 +66,7 @@ func Percent(val, total float64, n int) float64 {
|
||||
return result
|
||||
}
|
||||
|
||||
// RoundToString round up to n decimal places.
|
||||
// RoundToString round off to n decimal places.
|
||||
// Play: https://go.dev/play/p/kZwpBRAcllO
|
||||
func RoundToString[T constraints.Float | constraints.Integer](x T, n int) string {
|
||||
tmp := math.Pow(10.0, float64(n))
|
||||
@@ -76,7 +76,7 @@ func RoundToString[T constraints.Float | constraints.Integer](x T, n int) string
|
||||
return result
|
||||
}
|
||||
|
||||
// RoundToFloat round up to n decimal places.
|
||||
// RoundToFloat round off to n decimal places.
|
||||
// Play: https://go.dev/play/p/ghyb528JRJL
|
||||
func RoundToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 {
|
||||
tmp := math.Pow(10.0, float64(n))
|
||||
@@ -100,6 +100,40 @@ func TruncRound[T constraints.Float | constraints.Integer](x T, n int) T {
|
||||
return T(result)
|
||||
}
|
||||
|
||||
// FloorToFloat round down to n decimal places.
|
||||
func FloorToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 {
|
||||
tmp := math.Pow(10.0, float64(n))
|
||||
x *= T(tmp)
|
||||
r := math.Floor(float64(x))
|
||||
return r / tmp
|
||||
}
|
||||
|
||||
// FloorToString round down to n decimal places.
|
||||
func FloorToString[T constraints.Float | constraints.Integer](x T, n int) string {
|
||||
tmp := math.Pow(10.0, float64(n))
|
||||
x *= T(tmp)
|
||||
r := math.Floor(float64(x))
|
||||
result := strconv.FormatFloat(r/tmp, 'f', n, 64)
|
||||
return result
|
||||
}
|
||||
|
||||
// CeilToFloat round up to n decimal places.
|
||||
func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 {
|
||||
tmp := math.Pow(10.0, float64(n))
|
||||
x *= T(tmp)
|
||||
r := math.Ceil(float64(x))
|
||||
return r / tmp
|
||||
}
|
||||
|
||||
// CeilToString round up to n decimal places.
|
||||
func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string {
|
||||
tmp := math.Pow(10.0, float64(n))
|
||||
x *= T(tmp)
|
||||
r := math.Ceil(float64(x))
|
||||
result := strconv.FormatFloat(r/tmp, 'f', n, 64)
|
||||
return result
|
||||
}
|
||||
|
||||
// Max return max value of numbers.
|
||||
// Play: https://go.dev/play/p/cN8DHI0rTkH
|
||||
func Max[T constraints.Integer | constraints.Float](numbers ...T) T {
|
||||
@@ -253,7 +287,7 @@ func PointDistance(x1, y1, x2, y2 float64) float64 {
|
||||
return math.Sqrt(c)
|
||||
}
|
||||
|
||||
// IsPrimes checks if number is prime number.
|
||||
// IsPrime checks if number is prime number.
|
||||
// Play: https://go.dev/play/p/Rdd8UTHZJ7u
|
||||
func IsPrime(n int) bool {
|
||||
if n < 2 {
|
||||
@@ -351,3 +385,8 @@ func Abs[T constraints.Integer | constraints.Float](x T) T {
|
||||
|
||||
return x
|
||||
}
|
||||
|
||||
// Div returns the result of x divided by y.
|
||||
func Div[T constraints.Float | constraints.Integer](x T, y T) float64 {
|
||||
return float64(x) / float64(y)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user