作者 yangfu

fix: decimal problem

@@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
4 "fmt" 4 "fmt"
5 "github.com/linmadan/egglib-go/utils/xtime" 5 "github.com/linmadan/egglib-go/utils/xtime"
6 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils" 6 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
  7 + "strconv"
7 "strings" 8 "strings"
8 ) 9 )
9 10
@@ -221,7 +222,8 @@ func ToFieldData(fields []*Field, data [][]string, byName bool, configs ...bool) @@ -221,7 +222,8 @@ func ToFieldData(fields []*Field, data [][]string, byName bool, configs ...bool)
221 continue 222 continue
222 } 223 }
223 if len(d) >= j { 224 if len(d) >= j {
224 - item[key] = d[j] 225 + // 处理精度问题
  226 + item[key] = RoundFieldValue(f, d[j]) //d[j]
225 } else { 227 } else {
226 item[key] = "" 228 item[key] = ""
227 } 229 }
@@ -231,6 +233,19 @@ func ToFieldData(fields []*Field, data [][]string, byName bool, configs ...bool) @@ -231,6 +233,19 @@ func ToFieldData(fields []*Field, data [][]string, byName bool, configs ...bool)
231 return result 233 return result
232 } 234 }
233 235
  236 +// RoundFieldValue 字段值精度处理
  237 +func RoundFieldValue(f *Field, v string) string {
  238 + if f.SQLType != Float.ToString() {
  239 + return v
  240 + }
  241 + fv, err := strconv.ParseFloat(v, 64)
  242 + if err != nil {
  243 + return v
  244 + }
  245 + fv = utils.Round(fv, 6)
  246 + return fmt.Sprintf("%v", fv)
  247 +}
  248 +
234 func GripData(data []map[string]string, total int64) map[string]interface{} { 249 func GripData(data []map[string]string, total int64) map[string]interface{} {
235 if len(data) == 0 { 250 if len(data) == 0 {
236 data = make([]map[string]string, 0) 251 data = make([]map[string]string, 0)
@@ -257,6 +272,10 @@ func MakeToInterfaces(fields []*Field) func([]string) []interface{} { @@ -257,6 +272,10 @@ func MakeToInterfaces(fields []*Field) func([]string) []interface{} {
257 output := make([]interface{}, len(input)) 272 output := make([]interface{}, len(input))
258 for i, v := range input { 273 for i, v := range input {
259 if i < len(fields) { 274 if i < len(fields) {
  275 + // 处理精度问题
  276 + if fields[i].SQLType == Float.ToString() {
  277 + v = RoundFieldValue(fields[i], v)
  278 + }
260 convValue, err := ValueToType(v, fields[i].SQLType) 279 convValue, err := ValueToType(v, fields[i].SQLType)
261 if err == nil { 280 if err == nil {
262 output[i] = convValue 281 output[i] = convValue
  1 +package domain
  2 +
  3 +import (
  4 + "github.com/stretchr/testify/assert"
  5 + "testing"
  6 +)
  7 +
  8 +func TestRoundFieldValue(t *testing.T) {
  9 + floatFiled := &Field{
  10 + SQLType: Float.ToString(),
  11 + }
  12 + inputs := []struct {
  13 + v string
  14 + f *Field
  15 + want string
  16 + }{
  17 + {
  18 + v: "0.12359999999999999",
  19 + f: floatFiled,
  20 + want: "0.1236",
  21 + },
  22 + {
  23 + v: "0.12360000000000001",
  24 + f: floatFiled,
  25 + want: "0.1236",
  26 + },
  27 + {
  28 + v: "0.12359999999999995",
  29 + f: floatFiled,
  30 + want: "0.1236",
  31 + },
  32 + }
  33 + for _, input := range inputs {
  34 + got := RoundFieldValue(input.f, input.v)
  35 + assert.Equal(t, input.want, got)
  36 + }
  37 +}
@@ -217,7 +217,6 @@ func FastDataTable(options starrocks.QueryOptions) (*domain.DataTable, error) { @@ -217,7 +217,6 @@ func FastDataTable(options starrocks.QueryOptions) (*domain.DataTable, error) {
217 if err != nil { 217 if err != nil {
218 return nil, err 218 return nil, err
219 } 219 }
220 -  
221 dataTable.Total, err = starrocks.WrapQueryCountWithDB(options, starrocks.DB)() 220 dataTable.Total, err = starrocks.WrapQueryCountWithDB(options, starrocks.DB)()
222 if err != nil { 221 if err != nil {
223 return nil, err 222 return nil, err