Skip to content

Commit

Permalink
Preserve decimal precision when converting scientific notation to pla…
Browse files Browse the repository at this point in the history
…in notation number
  • Loading branch information
tommysitu committed Nov 30, 2024
1 parent 23b405d commit 055c73b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
28 changes: 9 additions & 19 deletions core/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions core/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`)
Expand Down

0 comments on commit 055c73b

Please sign in to comment.