mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(mathutil): format TruncRound integer inputs as floats (#365)
This commit is contained in:
1 parent
f220ea4c3f
commit
a296281408
2 files changed
+7
-1
No files matched your search
@@ -93,7 +93,7 @@ func RoundToFloat[T constraints.Float | constraints.Integer](x T, n int) float64
|
|||||||
// TruncRound round off n decimal places.
|
// TruncRound round off n decimal places.
|
||||||
// Play: https://go.dev/play/p/aumarSHIGzP
|
// Play: https://go.dev/play/p/aumarSHIGzP
|
||||||
func TruncRound[T constraints.Float | constraints.Integer](x T, n int) T {
|
func TruncRound[T constraints.Float | constraints.Integer](x T, n int) T {
|
||||||
floatStr := fmt.Sprintf("%."+strconv.Itoa(n+1)+"f", x)
|
floatStr := fmt.Sprintf("%."+strconv.Itoa(n+1)+"f", float64(x))
|
||||||
temp := strings.Split(floatStr, ".")
|
temp := strings.Split(floatStr, ".")
|
||||||
var newFloat string
|
var newFloat string
|
||||||
if len(temp) < 2 || n >= len(temp[1]) {
|
if len(temp) < 2 || n >= len(temp[1]) {
|
||||||
|
|||||||
@@ -17,6 +17,12 @@ func TestExponent(t *testing.T) {
|
|||||||
assert.Equal(int64(100), Exponent(10, 2))
|
assert.Equal(int64(100), Exponent(10, 2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTruncRoundInteger(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTruncRoundInteger")
|
||||||
|
assert.Equal(42, TruncRound(42, 0))
|
||||||
|
assert.Equal(int8(-42), TruncRound(int8(-42), 2))
|
||||||
|
}
|
||||||
|
|
||||||
func TestLCMIntermediateOverflow(t *testing.T) {
|
func TestLCMIntermediateOverflow(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestLCMIntermediateOverflow")
|
assert := internal.NewAssert(t, "TestLCMIntermediateOverflow")
|
||||||
assert.Equal(uint8(32), LCM(uint8(32)))
|
assert.Equal(uint8(32), LCM(uint8(32)))
|
||||||
|
|||||||
Reference in new issue
Block a user