Skip to content

Commit

Permalink
This closes #1979, fix decimal value round issue
Browse files Browse the repository at this point in the history
  • Loading branch information
centurion-hub committed Aug 31, 2024
1 parent 9c460ff commit 0304e38
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
47 changes: 26 additions & 21 deletions numfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions numfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit 0304e38

Please sign in to comment.