From f753e560fa975f2dd671382ac07203f284ce945c Mon Sep 17 00:00:00 2001 From: magicrabbit <31507468+phperic@users.noreply.github.com> Date: Sat, 4 Nov 2023 08:02:09 +0800 Subject: [PATCH] Fix number format scientific notation zero fill issues (#1710) --- numfmt.go | 7 +++---- numfmt_test.go | 2 ++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/numfmt.go b/numfmt.go index a6f456e203..a303b640f9 100644 --- a/numfmt.go +++ b/numfmt.go @@ -4953,7 +4953,7 @@ func (nf *numberFormat) numberHandler() string { fracLen = nf.fracPadding } if isNum, precision, decimal := isNumeric(nf.value); isNum { - if precision > 15 && intLen+fracLen > 15 { + if precision > 15 && intLen+fracLen > 15 && !nf.useScientificNotation { return nf.printNumberLiteral(nf.printBigNumber(decimal, fracLen)) } } @@ -4961,14 +4961,13 @@ func (nf *numberFormat) numberHandler() string { if fracLen > 0 { paddingLen++ } - flag := "f" + fmtCode := fmt.Sprintf("%%0%d.%df%s", paddingLen, fracLen, strings.Repeat("%%", nf.percent)) if nf.useScientificNotation { if nf.expBaseLen != 2 { return nf.value } - flag = "E" + fmtCode = fmt.Sprintf("%%.%dE%s", fracLen, strings.Repeat("%%", nf.percent)) } - fmtCode := fmt.Sprintf("%%0%d.%d%s%s", paddingLen, fracLen, flag, strings.Repeat("%%", nf.percent)) if nf.percent > 0 { num *= math.Pow(100, float64(nf.percent)) } diff --git a/numfmt_test.go b/numfmt_test.go index 45d7d0f0d0..b449feb44c 100644 --- a/numfmt_test.go +++ b/numfmt_test.go @@ -3539,6 +3539,8 @@ func TestNumFmt(t *testing.T) { {"8.8888666665555493e+19", "#,000.00", "88,888,666,665,555,500,000.00"}, {"8.8888666665555493e+19", "0.00000", "88888666665555500000.00000"}, {"37947.7500001", "0.00000000E+00", "3.79477500E+04"}, + {"2312312321.1231198", "0.00E+00", "2.31E+09"}, + {"3.2234623764278598E+33", "0.00E+00", "3.22E+33"}, {"1.234E-16", "0.00000000000000000000", "0.00000000000000012340"}, {"1.234E-16", "0.000000000000000000", "0.000000000000000123"}, {"1.234E-16", "0.000000000000000000%", "0.000000000000012340%"},