From 055c73bc910643e388b3f7afc208d30095b461fe Mon Sep 17 00:00:00 2001 From: Tommy Situ Date: Sat, 30 Nov 2024 20:07:31 +0000 Subject: [PATCH] Preserve decimal precision when converting scientific notation to plain notation number --- core/util/util.go | 28 +++++++++------------------- core/util/util_test.go | 8 ++++---- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/core/util/util.go b/core/util/util.go index 661710a3d..ccc9ed168 100644 --- a/core/util/util.go +++ b/core/util/util.go @@ -402,13 +402,10 @@ func jsonPath(query, toMatch string) interface{} { // Jsonpath library converts large int into a string with scientific notion, the following // reverts that process to avoid mismatching when using the jsonpath result for csv data lookup - // Handle large integers in scientific notation by converting back to big.Int - if isScientific(result) { - // If result is in scientific notation, try converting to a big.Int - bigInt := new(big.Int) - bigInt, success := bigIntFromString(result) - if success { - result = bigInt.String() // Convert back to string representation of the big integer + if containScientificNotation(result) { + plainNotation, ok := convertToPlainNotation(result) + if ok { + result = plainNotation } } @@ -425,24 +422,17 @@ func jsonPath(query, toMatch string) interface{} { } // isScientific checks if a string is in scientific notation (e.g., "1.349599e+37") -func isScientific(value string) bool { +func containScientificNotation(value string) bool { return strings.Contains(value, "e") || strings.Contains(value, "E") } -// bigIntFromString converts a string representing a number (potentially in scientific notation) to big.Int -func bigIntFromString(value string) (*big.Int, bool) { - // Parse the string as a big.Float to handle scientific notation - flt := new(big.Float) - flt, _, err := big.ParseFloat(value, 10, 0, big.ToNearestEven) +func convertToPlainNotation(scientific string) (string, bool) { + floatVal, _, err := big.ParseFloat(scientific, 10, 0, big.ToNearestEven) if err != nil { - return nil, false + return scientific, false } - // Convert the big.Float to big.Int (rounding down) - bigInt := new(big.Int) - flt.Int(bigInt) - - return bigInt, true + return floatVal.Text('f', -1) , true } func xPath(query, toMatch string) string { diff --git a/core/util/util_test.go b/core/util/util_test.go index 4ebac73fd..40c59270c 100644 --- a/core/util/util_test.go +++ b/core/util/util_test.go @@ -237,18 +237,18 @@ func Test_CopyMap(t *testing.T) { func Test_JsonPathMethod_WithBigFloatingNumber(t *testing.T) { RegisterTestingT(t) - res := jsonPath("$.registrant", `{"registrant":"13495985898986869898697879879987978978.12345566777"}`) - Expect(res).To(Equal("13495985898986869898697879879987978978.12345566777")) + res := jsonPath("$.registrant", `{"registrant":2343534534534.345}`) + Expect(res).To(Equal("2343534534534.345")) } -func Test_JsonPathMethod_WithBigIntegerUserProvidedNumber(t *testing.T) { +func Test_JsonPathMethod_WithStringContainingLargeNumber(t *testing.T) { RegisterTestingT(t) res := jsonPath("$.registrant", `{"registrant":"0009007199254740999"}`) Expect(res).To(Equal("0009007199254740999")) } -func Test_JsonPathMethod_WithWordContainingEe(t *testing.T) { +func Test_JsonPathMethod_WithWordContainingLetterE(t *testing.T) { RegisterTestingT(t) res := jsonPath("$.registrant", `{"registrant":"ETest"}`)