diff --git a/numfmt.go b/numfmt.go index f34707b84e..cae88d64ab 100644 --- a/numfmt.go +++ b/numfmt.go @@ -4838,12 +4838,26 @@ func format(value, numFmt string, date1904 bool, cellType CellType, opts *Option // getNumberPartLen returns the length of integer and fraction parts for the // numeric. -func getNumberPartLen(n float64) (int, int) { - parts := strings.Split(strconv.FormatFloat(math.Abs(n), 'f', -1, 64), ".") +func (nf *numberFormat) getNumberPartLen() (int, int) { + var intPart, fracPart, intLen, fracLen int + parts := strings.Split(strconv.FormatFloat(math.Abs(nf.number), 'f', -1, 64), ".") + intPart = len(parts[0]) if len(parts) == 2 { - return len(parts[0]), len(parts[1]) + fracPart = len(parts[1]) } - return len(parts[0]), 0 + if nf.intHolder > intPart { + nf.intHolder = intPart + } + if intLen = intPart; nf.intPadding+nf.intHolder > intPart { + intLen = nf.intPadding + nf.intHolder + } + if fracLen = fracPart; fracPart > nf.fracHolder+nf.fracPadding { + fracLen = nf.fracHolder + nf.fracPadding + } + if nf.fracPadding > fracPart { + fracLen = nf.fracPadding + } + return intLen, fracLen } // getNumberFmtConf generate the number format padding and placeholder @@ -5021,25 +5035,12 @@ func (nf *numberFormat) printBigNumber(decimal float64, fracLen int) string { // numberHandler handling number format expression for positive and negative // numeric. func (nf *numberFormat) numberHandler() string { + nf.getNumberFmtConf() var ( - num = nf.number - intPart, fracPart = getNumberPartLen(nf.number) - intLen, fracLen int - result string + num = nf.number + intLen, fracLen = nf.getNumberPartLen() + result string ) - nf.getNumberFmtConf() - if nf.intHolder > intPart { - nf.intHolder = intPart - } - if intLen = intPart; nf.intPadding+nf.intHolder > intPart { - intLen = nf.intPadding + nf.intHolder - } - if fracLen = fracPart; fracPart > nf.fracHolder+nf.fracPadding { - fracLen = nf.fracHolder + nf.fracPadding - } - if nf.fracPadding > fracPart { - fracLen = nf.fracPadding - } if isNum, precision, decimal := isNumeric(nf.value); isNum { if precision > 15 && intLen+fracLen > 15 && !nf.useScientificNotation { return nf.printNumberLiteral(nf.printBigNumber(decimal, fracLen)) @@ -5062,6 +5063,10 @@ func (nf *numberFormat) numberHandler() string { if nf.useFraction { num = math.Floor(math.Abs(num)) } + if !nf.useScientificNotation { + ratio := math.Pow(10, float64(fracLen)) + num = math.Round(num*ratio) / ratio + } if result = fmt.Sprintf(fmtCode, math.Abs(num)); nf.useCommaSep { result = printCommaSep(result) } diff --git a/numfmt_test.go b/numfmt_test.go index 21a07d26fe..61bf41bb48 100644 --- a/numfmt_test.go +++ b/numfmt_test.go @@ -3553,6 +3553,7 @@ func TestNumFmt(t *testing.T) { {"-8.04506", "$#,##0.00_);[Red]($#,##0.00)", "($8.05)"}, {"43543.5448726851", `_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)`, " $43,543.54 "}, {"1234.5678", "0", "1235"}, + {"1234.125", "0.00", "1234.13"}, {"1234.5678", "0.00", "1234.57"}, {"1234.5678", "#,##0", "1,235"}, {"1234.5678", "#,##0.00", "1,234.57"},