Skip to content

Commit e6bf0ee

Browse files
netomisparkprime
authored andcommitted
Fix format with general format type.
1 parent d85b863 commit e6bf0ee

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

stdlib/std.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ limitations under the License.
657657
error 'Format required number at '
658658
+ i + ', got ' + std.type(val)
659659
else
660-
local exponent = std.floor(std.log(std.abs(val)) / std.log(10));
660+
local exponent = if val != 0 then std.floor(std.log(std.abs(val)) / std.log(10)) else 0;
661661
if exponent < -4 || exponent >= fpprec then
662662
render_float_sci(val,
663663
zp,

test_suite/format.jsonnet

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ std.assertEqual(std.format('%.1f', [1.95555]), '2.0') &&
216216
std.assertEqual(std.format('%.4f', [0.99995]), '1.0000') &&
217217

218218
// g
219+
220+
std.assertEqual(std.format('%g', [0]), '0') &&
219221
std.assertEqual(std.format('%#.3g', [1000000001]), '1.00e+09') &&
220222
std.assertEqual(std.format('%#.3g', [1100]), '1.10e+03') &&
221223
std.assertEqual(std.format('%#.3g', [1.1]), '1.10') &&
@@ -249,6 +251,7 @@ std.assertEqual(std.format('%10.5g', [110]), ' 110') &&
249251
std.assertEqual(std.format('%10.5g', [1.1]), ' 1.1') &&
250252

251253
// G
254+
std.assertEqual(std.format('%G', [0]), '0') &&
252255
std.assertEqual(std.format('%#.3G', [1000000001]), '1.00E+09') &&
253256
std.assertEqual(std.format('%#.3G', [1100]), '1.10E+03') &&
254257
std.assertEqual(std.format('%#.3G', [1.1]), '1.10') &&

0 commit comments

Comments
 (0)