Skip to content

Commit 8f87131

Browse files
This closes qax-os#1979, fix decimal value round issue (qax-os#1984)
- Updated unit tests
1 parent 9c460ff commit 8f87131

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

numfmt.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4838,12 +4838,26 @@ func format(value, numFmt string, date1904 bool, cellType CellType, opts *Option
48384838

48394839
// getNumberPartLen returns the length of integer and fraction parts for the
48404840
// numeric.
4841-
func getNumberPartLen(n float64) (int, int) {
4842-
parts := strings.Split(strconv.FormatFloat(math.Abs(n), 'f', -1, 64), ".")
4841+
func (nf *numberFormat) getNumberPartLen() (int, int) {
4842+
var intPart, fracPart, intLen, fracLen int
4843+
parts := strings.Split(strconv.FormatFloat(math.Abs(nf.number), 'f', -1, 64), ".")
4844+
intPart = len(parts[0])
48434845
if len(parts) == 2 {
4844-
return len(parts[0]), len(parts[1])
4846+
fracPart = len(parts[1])
48454847
}
4846-
return len(parts[0]), 0
4848+
if nf.intHolder > intPart {
4849+
nf.intHolder = intPart
4850+
}
4851+
if intLen = intPart; nf.intPadding+nf.intHolder > intPart {
4852+
intLen = nf.intPadding + nf.intHolder
4853+
}
4854+
if fracLen = fracPart; fracPart > nf.fracHolder+nf.fracPadding {
4855+
fracLen = nf.fracHolder + nf.fracPadding
4856+
}
4857+
if nf.fracPadding > fracPart {
4858+
fracLen = nf.fracPadding
4859+
}
4860+
return intLen, fracLen
48474861
}
48484862

48494863
// getNumberFmtConf generate the number format padding and placeholder
@@ -5021,25 +5035,12 @@ func (nf *numberFormat) printBigNumber(decimal float64, fracLen int) string {
50215035
// numberHandler handling number format expression for positive and negative
50225036
// numeric.
50235037
func (nf *numberFormat) numberHandler() string {
5038+
nf.getNumberFmtConf()
50245039
var (
5025-
num = nf.number
5026-
intPart, fracPart = getNumberPartLen(nf.number)
5027-
intLen, fracLen int
5028-
result string
5040+
num = nf.number
5041+
intLen, fracLen = nf.getNumberPartLen()
5042+
result string
50295043
)
5030-
nf.getNumberFmtConf()
5031-
if nf.intHolder > intPart {
5032-
nf.intHolder = intPart
5033-
}
5034-
if intLen = intPart; nf.intPadding+nf.intHolder > intPart {
5035-
intLen = nf.intPadding + nf.intHolder
5036-
}
5037-
if fracLen = fracPart; fracPart > nf.fracHolder+nf.fracPadding {
5038-
fracLen = nf.fracHolder + nf.fracPadding
5039-
}
5040-
if nf.fracPadding > fracPart {
5041-
fracLen = nf.fracPadding
5042-
}
50435044
if isNum, precision, decimal := isNumeric(nf.value); isNum {
50445045
if precision > 15 && intLen+fracLen > 15 && !nf.useScientificNotation {
50455046
return nf.printNumberLiteral(nf.printBigNumber(decimal, fracLen))
@@ -5062,6 +5063,10 @@ func (nf *numberFormat) numberHandler() string {
50625063
if nf.useFraction {
50635064
num = math.Floor(math.Abs(num))
50645065
}
5066+
if !nf.useScientificNotation {
5067+
ratio := math.Pow(10, float64(fracLen))
5068+
num = math.Round(num*ratio) / ratio
5069+
}
50655070
if result = fmt.Sprintf(fmtCode, math.Abs(num)); nf.useCommaSep {
50665071
result = printCommaSep(result)
50675072
}

numfmt_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,6 +3553,7 @@ func TestNumFmt(t *testing.T) {
35533553
{"-8.04506", "$#,##0.00_);[Red]($#,##0.00)", "($8.05)"},
35543554
{"43543.5448726851", `_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)`, " $43,543.54 "},
35553555
{"1234.5678", "0", "1235"},
3556+
{"1234.125", "0.00", "1234.13"},
35563557
{"1234.5678", "0.00", "1234.57"},
35573558
{"1234.5678", "#,##0", "1,235"},
35583559
{"1234.5678", "#,##0.00", "1,234.57"},

0 commit comments

Comments
 (0)