From 89d17d54ce7b9b4822d518db568fdae128ee5aee Mon Sep 17 00:00:00 2001 From: sueun-dev Date: Tue, 23 Jun 2026 09:35:12 +0900 Subject: [PATCH] Preserve full precision for non-integer numbers in jq output 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. --- pkg/jq/jq.go | 2 +- pkg/jq/jq_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/jq/jq.go b/pkg/jq/jq.go index 5f19e1f6..d49e3f57 100644 --- a/pkg/jq/jq.go +++ b/pkg/jq/jq.go @@ -104,7 +104,7 @@ func jsonScalarToString(input interface{}) (string, error) { if math.Trunc(tt) == tt { return strconv.FormatFloat(tt, 'f', 0, 64), nil } else { - return strconv.FormatFloat(tt, 'f', 2, 64), nil + return strconv.FormatFloat(tt, 'f', -1, 64), nil } case nil: return "", nil diff --git a/pkg/jq/jq_test.go b/pkg/jq/jq_test.go index 1264de97..4dc5b726 100644 --- a/pkg/jq/jq_test.go +++ b/pkg/jq/jq_test.go @@ -172,6 +172,16 @@ func TestEvaluateFormatted(t *testing.T) { " \x1b[32m\"bar\"\x1b[m\n" + "\x1b[1;38m}\x1b[m\n", }, + { + name: "non-integer numbers keep full precision", + args: args{ + json: strings.NewReader(`{"pi": 3.14159, "small": 0.001, "big": 123456.789}`), + expr: `.pi, .small, .big`, + indent: "", + colorize: false, + }, + wantW: "3.14159\n0.001\n123456.789\n", + }, { name: "halt function", args: args{