From 31b17e83a66a629c5b0cf41e191ea003be89fc95 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 3 Jul 2024 22:09:47 +0900 Subject: [PATCH] fix(fmt): fix the case when mantissa exceeds 10 by rounding and exponent is negative --- fmt/printf.ts | 2 +- fmt/printf_test.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) 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 2c49b7ad7d99..6b143125f104 100644 --- a/fmt/printf_test.ts +++ b/fmt/printf_test.ts @@ -108,6 +108,7 @@ Deno.test("sprintf() handles floats", function () { assertEquals(sprintf("%e", Number.MIN_SAFE_INTEGER), "-9.007199e+15"); assertEquals(sprintf("%.3e", 1.9999), "2.000e+00"); assertEquals(sprintf("%.3e", 29.99999), "3.000e+01"); + assertEquals(sprintf("%.3e", 0.000099999), "1.000e-04"); }); Deno.test("sprintf() handles floatE", function () { assertEquals(sprintf("%E", 4), "4.000000E+00");