mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-09 03:50:26 +08:00
优化小数数字格式化
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func FormatInt64(value int64) string {
|
func FormatInt64(value int64) string {
|
||||||
@@ -50,15 +49,5 @@ func FloorFloat32(f float32, decimal int) float32 {
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
var s = fmt.Sprintf("%f", f)
|
return types.Float32(fmt.Sprintf("%.2f", f))
|
||||||
var index = strings.Index(s, ".")
|
|
||||||
if index < 0 {
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
|
|
||||||
var d = s[index:]
|
|
||||||
if len(d) <= decimal+1 {
|
|
||||||
return f
|
|
||||||
}
|
|
||||||
return types.Float32(s[:index] + d[:decimal+1])
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,3 +35,9 @@ func TestFloorFloat32(t *testing.T) {
|
|||||||
t.Logf("%f, %f", numberutils.FloorFloat32(123.456789, 4), 123.456789*10*10*10*10)
|
t.Logf("%f, %f", numberutils.FloorFloat32(123.456789, 4), 123.456789*10*10*10*10)
|
||||||
t.Logf("%f", numberutils.FloorFloat32(-123.45678, 2))
|
t.Logf("%f", numberutils.FloorFloat32(-123.45678, 2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFloorFloat32_Special(t *testing.T) {
|
||||||
|
for _, f := range []float32{17.88, 1.11, 1.23456} {
|
||||||
|
t.Logf("%f", numberutils.FloorFloat32(f, 2))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user