From 96aa98e305a108da5d38f06bb3894324ef50cec4 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Tue, 18 Apr 2023 07:59:26 +0200 Subject: [PATCH] Fix format with general format type. --- stdlib/std.jsonnet | 2 +- test_suite/format.jsonnet | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/stdlib/std.jsonnet b/stdlib/std.jsonnet index 960deeb0f..8178b6740 100644 --- a/stdlib/std.jsonnet +++ b/stdlib/std.jsonnet @@ -657,7 +657,7 @@ limitations under the License. error 'Format required number at ' + i + ', got ' + std.type(val) else - local exponent = std.floor(std.log(std.abs(val)) / std.log(10)); + local exponent = if val != 0 then std.floor(std.log(std.abs(val)) / std.log(10)) else 0; if exponent < -4 || exponent >= fpprec then render_float_sci(val, zp, diff --git a/test_suite/format.jsonnet b/test_suite/format.jsonnet index eb04c8639..a7b8f0620 100644 --- a/test_suite/format.jsonnet +++ b/test_suite/format.jsonnet @@ -216,6 +216,8 @@ std.assertEqual(std.format('%.1f', [1.95555]), '2.0') && std.assertEqual(std.format('%.4f', [0.99995]), '1.0000') && // g + +std.assertEqual(std.format('%g', [0]), '0') && std.assertEqual(std.format('%#.3g', [1000000001]), '1.00e+09') && std.assertEqual(std.format('%#.3g', [1100]), '1.10e+03') && std.assertEqual(std.format('%#.3g', [1.1]), '1.10') && @@ -249,6 +251,7 @@ std.assertEqual(std.format('%10.5g', [110]), ' 110') && std.assertEqual(std.format('%10.5g', [1.1]), ' 1.1') && // G +std.assertEqual(std.format('%G', [0]), '0') && std.assertEqual(std.format('%#.3G', [1000000001]), '1.00E+09') && std.assertEqual(std.format('%#.3G', [1100]), '1.10E+03') && std.assertEqual(std.format('%#.3G', [1.1]), '1.10') &&