Keep full precision for non-integer numbers in jq output#255
Open
sueun-dev wants to merge 1 commit into
Open
Conversation
jsonScalarToString formatted non-integer float64 values with two decimal places, so jq.Evaluate truncated them: 3.14159 became 3.14 and 0.001 became 0.00. That contradicts the package's documented behavior of writing values 'similar to how jq --raw works', and the 0.001 -> 0.00 case turns a nonzero value into zero. Use FormatFloat with precision -1, which produces the shortest string that round-trips the float64. Integer-valued floats are unchanged.
Member
|
Thanks for flagging this, @sueun-dev! 🙏 Can you please open an issue first, so that we can have a thread to discuss this? |
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
jq.Evaluaterounds non-integer numbers to two decimal places, so the output loses precision and can even turn a nonzero value into zero.For input
{"pi": 3.14159, "small": 0.001, "big": 123456.789}, evaluating.pi, .small, .bigprints:jsonScalarToStringformats non-integerfloat64values withstrconv.FormatFloat(tt, 'f', 2, 64). The package doc says scalars are written "similar to how jq --raw works", butjqprints3.14159,0.001, and123456.789. The0.001 -> 0.00case is the worst one, since a nonzero number renders as zero.The non-integer branch now uses precision
-1, the shortest string that round-trips thefloat64, so the three values above print exactly asjqdoes. The integer-valued branch is unchanged, so whole numbers still print without a trailing.0.Added a case to
TestEvaluateFormattedcovering3.14159,0.001, and123456.789. It fails on the current code and passes with the change.go test ./...,go vet, andgofmt -lare clean.