Skip to content

Commit 055c73b

Browse files
committed
Preserve decimal precision when converting scientific notation to plain notation number
1 parent 23b405d commit 055c73b

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

core/util/util.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -402,13 +402,10 @@ func jsonPath(query, toMatch string) interface{} {
402402

403403
// Jsonpath library converts large int into a string with scientific notion, the following
404404
// reverts that process to avoid mismatching when using the jsonpath result for csv data lookup
405-
// Handle large integers in scientific notation by converting back to big.Int
406-
if isScientific(result) {
407-
// If result is in scientific notation, try converting to a big.Int
408-
bigInt := new(big.Int)
409-
bigInt, success := bigIntFromString(result)
410-
if success {
411-
result = bigInt.String() // Convert back to string representation of the big integer
405+
if containScientificNotation(result) {
406+
plainNotation, ok := convertToPlainNotation(result)
407+
if ok {
408+
result = plainNotation
412409
}
413410
}
414411

@@ -425,24 +422,17 @@ func jsonPath(query, toMatch string) interface{} {
425422
}
426423

427424
// isScientific checks if a string is in scientific notation (e.g., "1.349599e+37")
428-
func isScientific(value string) bool {
425+
func containScientificNotation(value string) bool {
429426
return strings.Contains(value, "e") || strings.Contains(value, "E")
430427
}
431428

432-
// bigIntFromString converts a string representing a number (potentially in scientific notation) to big.Int
433-
func bigIntFromString(value string) (*big.Int, bool) {
434-
// Parse the string as a big.Float to handle scientific notation
435-
flt := new(big.Float)
436-
flt, _, err := big.ParseFloat(value, 10, 0, big.ToNearestEven)
429+
func convertToPlainNotation(scientific string) (string, bool) {
430+
floatVal, _, err := big.ParseFloat(scientific, 10, 0, big.ToNearestEven)
437431
if err != nil {
438-
return nil, false
432+
return scientific, false
439433
}
440434

441-
// Convert the big.Float to big.Int (rounding down)
442-
bigInt := new(big.Int)
443-
flt.Int(bigInt)
444-
445-
return bigInt, true
435+
return floatVal.Text('f', -1) , true
446436
}
447437

448438
func xPath(query, toMatch string) string {

core/util/util_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,18 @@ func Test_CopyMap(t *testing.T) {
237237
func Test_JsonPathMethod_WithBigFloatingNumber(t *testing.T) {
238238

239239
RegisterTestingT(t)
240-
res := jsonPath("$.registrant", `{"registrant":"13495985898986869898697879879987978978.12345566777"}`)
241-
Expect(res).To(Equal("13495985898986869898697879879987978978.12345566777"))
240+
res := jsonPath("$.registrant", `{"registrant":2343534534534.345}`)
241+
Expect(res).To(Equal("2343534534534.345"))
242242
}
243243

244-
func Test_JsonPathMethod_WithBigIntegerUserProvidedNumber(t *testing.T) {
244+
func Test_JsonPathMethod_WithStringContainingLargeNumber(t *testing.T) {
245245

246246
RegisterTestingT(t)
247247
res := jsonPath("$.registrant", `{"registrant":"0009007199254740999"}`)
248248
Expect(res).To(Equal("0009007199254740999"))
249249
}
250250

251-
func Test_JsonPathMethod_WithWordContainingEe(t *testing.T) {
251+
func Test_JsonPathMethod_WithWordContainingLetterE(t *testing.T) {
252252

253253
RegisterTestingT(t)
254254
res := jsonPath("$.registrant", `{"registrant":"ETest"}`)

0 commit comments

Comments
 (0)