diff --git a/fmt/printf.ts b/fmt/printf.ts index 6c5bc78f18e9..7a9b3d6e076d 100644 --- a/fmt/printf.ts +++ b/fmt/printf.ts @@ -718,7 +718,7 @@ class Printf { if (10 <= mantissa) { mantissa = 1; const r = parseInt(esign + e) + 1; - e = r.toString(); + e = Math.abs(r).toString(); esign = r < 0 ? "-" : "+"; } } diff --git a/fmt/printf_test.ts b/fmt/printf_test.ts index 336710fcb83d..b31e563b2080 100644 --- a/fmt/printf_test.ts +++ b/fmt/printf_test.ts @@ -110,6 +110,7 @@ Deno.test("sprintf() handles floats", function () { assertEquals(sprintf("%.3e", 1.9999), "2.000e+00"); assertEquals(sprintf("%.3e", 29.99999), "3.000e+01"); assertEquals(sprintf("%.3e", 999999), "1.000e+06"); + assertEquals(sprintf("%.3e", 0.000099999), "1.000e-04"); }); Deno.test("sprintf() handles floatE", function () { assertEquals(sprintf("%E", 4), "4.000000E+00");