diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index e77167b..e27f430 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -77,8 +77,7 @@ func Percent(val, total float64, n int) float64 { // 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)) - x *= T(tmp) - r := math.Round(float64(x)) + r := math.Round(float64(x) * tmp) result := strconv.FormatFloat(r/tmp, 'f', n, 64) return result } @@ -87,8 +86,7 @@ func RoundToString[T constraints.Float | constraints.Integer](x T, n int) string // 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)) - x *= T(tmp) - r := math.Round(float64(x)) + r := math.Round(float64(x) * tmp) return r / tmp } @@ -111,8 +109,7 @@ func TruncRound[T constraints.Float | constraints.Integer](x T, n int) T { // Play: https://go.dev/play/p/vbCBrQHZEED 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)) + r := math.Floor(float64(x) * tmp) return r / tmp } @@ -120,8 +117,7 @@ func FloorToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 // Play: https://go.dev/play/p/Qk9KPd2IdDb 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)) + r := math.Floor(float64(x) * tmp) result := strconv.FormatFloat(r/tmp, 'f', n, 64) return result } @@ -130,8 +126,7 @@ func FloorToString[T constraints.Float | constraints.Integer](x T, n int) string // Play: https://go.dev/play/p/8hOeSADZPCo 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)) + r := math.Ceil(float64(x) * tmp) return r / tmp } @@ -139,8 +134,7 @@ func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 // Play: https://go.dev/play/p/wy5bYEyUKKG 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)) + r := math.Ceil(float64(x) * tmp) result := strconv.FormatFloat(r/tmp, 'f', n, 64) return result } diff --git a/mathutil/mathutil_test.go b/mathutil/mathutil_test.go index 1959ac1..869dbc9 100644 --- a/mathutil/mathutil_test.go +++ b/mathutil/mathutil_test.go @@ -66,6 +66,19 @@ func TestPercent(t *testing.T) { assert.EqualValues(-7.42, Percent(-30305, 408420, 2)) } +func TestIntegerRoundingScale(t *testing.T) { + assert := internal.NewAssert(t, "TestIntegerRoundingScale") + assert.Equal(float64(42), RoundToFloat(int8(42), 2)) + assert.Equal("42.00", RoundToString(int8(42), 2)) + assert.Equal(float64(42), FloorToFloat(uint8(42), 2)) + assert.Equal("42.00", FloorToString(uint8(42), 2)) + assert.Equal(float64(42), CeilToFloat(int8(42), 2)) + assert.Equal("42.00", CeilToString(int8(42), 2)) + assert.Equal(float64(150), RoundToFloat(149, -1)) + assert.Equal(float64(140), FloorToFloat(149, -1)) + assert.Equal(float64(150), CeilToFloat(149, -1)) +} + func TestRoundToFloat(t *testing.T) { t.Parallel()