Skip to content

Commit

Permalink
Fix number format scientific notation zero fill issues (#1710)
Browse files Browse the repository at this point in the history
  • Loading branch information
phperic committed Nov 4, 2023
1 parent 4e936da commit f753e56
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 3 additions & 4 deletions numfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4953,22 +4953,21 @@ 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))
}
}
paddingLen := intLen + fracLen
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))
}
Expand Down
2 changes: 2 additions & 0 deletions numfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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%"},
Expand Down

0 comments on commit f753e56

Please sign in to comment.