fix(mathutil): scale integer inputs after float conversion (#361)

This commit is contained in:
Yusuf Efe authored and GitHub committed 2026-10-23 21:02:24 +08:00
1 parent 172712df30
commit ec11fdf345
2 files changed
+19 -12

No files matched your search

+6 -12
View File
@@ -77,8 +77,7 @@ func Percent(val, total float64, n int) float64 {
// Play: https://go.dev/play/p/kZwpBRAcllO // Play: https://go.dev/play/p/kZwpBRAcllO
func RoundToString[T constraints.Float | constraints.Integer](x T, n int) string { func RoundToString[T constraints.Float | constraints.Integer](x T, n int) string {
tmp := math.Pow(10.0, float64(n)) tmp := math.Pow(10.0, float64(n))
x *= T(tmp) r := math.Round(float64(x) * tmp)
r := math.Round(float64(x))
result := strconv.FormatFloat(r/tmp, 'f', n, 64) result := strconv.FormatFloat(r/tmp, 'f', n, 64)
return result 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 // Play: https://go.dev/play/p/ghyb528JRJL
func RoundToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 { func RoundToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 {
tmp := math.Pow(10.0, float64(n)) tmp := math.Pow(10.0, float64(n))
x *= T(tmp) r := math.Round(float64(x) * tmp)
r := math.Round(float64(x))
return r / 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 // Play: https://go.dev/play/p/vbCBrQHZEED
func FloorToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 { func FloorToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 {
tmp := math.Pow(10.0, float64(n)) tmp := math.Pow(10.0, float64(n))
x *= T(tmp) r := math.Floor(float64(x) * tmp)
r := math.Floor(float64(x))
return r / 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 // Play: https://go.dev/play/p/Qk9KPd2IdDb
func FloorToString[T constraints.Float | constraints.Integer](x T, n int) string { func FloorToString[T constraints.Float | constraints.Integer](x T, n int) string {
tmp := math.Pow(10.0, float64(n)) tmp := math.Pow(10.0, float64(n))
x *= T(tmp) r := math.Floor(float64(x) * tmp)
r := math.Floor(float64(x))
result := strconv.FormatFloat(r/tmp, 'f', n, 64) result := strconv.FormatFloat(r/tmp, 'f', n, 64)
return result 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 // Play: https://go.dev/play/p/8hOeSADZPCo
func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 { func CeilToFloat[T constraints.Float | constraints.Integer](x T, n int) float64 {
tmp := math.Pow(10.0, float64(n)) tmp := math.Pow(10.0, float64(n))
x *= T(tmp) r := math.Ceil(float64(x) * tmp)
r := math.Ceil(float64(x))
return r / 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 // Play: https://go.dev/play/p/wy5bYEyUKKG
func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string { func CeilToString[T constraints.Float | constraints.Integer](x T, n int) string {
tmp := math.Pow(10.0, float64(n)) tmp := math.Pow(10.0, float64(n))
x *= T(tmp) r := math.Ceil(float64(x) * tmp)
r := math.Ceil(float64(x))
result := strconv.FormatFloat(r/tmp, 'f', n, 64) result := strconv.FormatFloat(r/tmp, 'f', n, 64)
return result return result
} }
+13
View File
@@ -66,6 +66,19 @@ func TestPercent(t *testing.T) {
assert.EqualValues(-7.42, Percent(-30305, 408420, 2)) 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) { func TestRoundToFloat(t *testing.T) {
t.Parallel() t.Parallel()