作者 yangfu

fix: decimal problem

... ... @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/linmadan/egglib-go/utils/xtime"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
"strconv"
"strings"
)
... ... @@ -221,7 +222,8 @@ func ToFieldData(fields []*Field, data [][]string, byName bool, configs ...bool)
continue
}
if len(d) >= j {
item[key] = d[j]
// 处理精度问题
item[key] = RoundFieldValue(f, d[j]) //d[j]
} else {
item[key] = ""
}
... ... @@ -231,6 +233,19 @@ func ToFieldData(fields []*Field, data [][]string, byName bool, configs ...bool)
return result
}
// RoundFieldValue 字段值精度处理
func RoundFieldValue(f *Field, v string) string {
if f.SQLType != Float.ToString() {
return v
}
fv, err := strconv.ParseFloat(v, 64)
if err != nil {
return v
}
fv = utils.Round(fv, 6)
return fmt.Sprintf("%v", fv)
}
func GripData(data []map[string]string, total int64) map[string]interface{} {
if len(data) == 0 {
data = make([]map[string]string, 0)
... ... @@ -257,6 +272,10 @@ func MakeToInterfaces(fields []*Field) func([]string) []interface{} {
output := make([]interface{}, len(input))
for i, v := range input {
if i < len(fields) {
// 处理精度问题
if fields[i].SQLType == Float.ToString() {
v = RoundFieldValue(fields[i], v)
}
convValue, err := ValueToType(v, fields[i].SQLType)
if err == nil {
output[i] = convValue
... ...
package domain
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestRoundFieldValue(t *testing.T) {
floatFiled := &Field{
SQLType: Float.ToString(),
}
inputs := []struct {
v string
f *Field
want string
}{
{
v: "0.12359999999999999",
f: floatFiled,
want: "0.1236",
},
{
v: "0.12360000000000001",
f: floatFiled,
want: "0.1236",
},
{
v: "0.12359999999999995",
f: floatFiled,
want: "0.1236",
},
}
for _, input := range inputs {
got := RoundFieldValue(input.f, input.v)
assert.Equal(t, input.want, got)
}
}
... ...
... ... @@ -217,7 +217,6 @@ func FastDataTable(options starrocks.QueryOptions) (*domain.DataTable, error) {
if err != nil {
return nil, err
}
dataTable.Total, err = starrocks.WrapQueryCountWithDB(options, starrocks.DB)()
if err != nil {
return nil, err
... ...