qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
18.05k stars 1.71k forks source link

File.CalcCellValue returns wrong value for "=ROUND(1444.00000000000003,2)" #1851

Closed alexbrainman-anz closed 6 months ago

alexbrainman-anz commented 6 months ago

Description

I am using

=ROUND(1444.00000000000003,2)

formula in my Excel files.

I am calling File.CalcCellValue method on those formulas with Options{RawCellValue: true} option. I expect to see no digits after decimal point, but I see 1444.0000000000002 returned.

Steps to reproduce the issue:

I used this commit 4eb088cf7 .

I made these changes to the code:

diff --git a/calc_test.go b/calc_test.go
index 71d0acd..2ce7908 100644
--- a/calc_test.go
+++ b/calc_test.go
@@ -6317,6 +6317,8 @@ func TestFormulaRawCellValueOption(t *testing.T) {
                {"=\"10\" & \"e3\"", true, "10e3"},
                {"=\"1111111111111111\"", false, "1.11111111111111E+15"},
                {"=\"1111111111111111\"", true, "1111111111111111"},
+               {"=ROUND(1444.00000000000003,2)", false, "1444"},
+               {"=ROUND(1444.00000000000003,2)", true, "1444"},
        }
        for _, test := range rawTest {
                assert.NoError(t, f.SetCellFormula("Sheet1", "A1", test.value))

I run this command:

go test -run=TestFormulaRawCellValueOption

Describe the results you received:

I see this output:

--- FAIL: TestFormulaRawCellValueOption (0.00s)
    calc_test.go:6327:
                Error Trace:    /Users/brainmaa/src/github.com/qax-os/excelize/calc_test.go:6327
                Error:          Not equal:
                                expected: "1444"
                                actual  : "1444.0000000000002"

                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1 +1 @@
                                -1444
                                +1444.0000000000002
                Test:           TestFormulaRawCellValueOption
FAIL
exit status 1
FAIL    github.com/xuri/excelize/v2     0.183s

Describe the results you expected:

PASS
ok    github.com/xuri/excelize/v2     0.183s

Output of go version:

go version go1.21.6 darwin/arm64

Excelize version or commit ID:

4eb088cf7

Environment details (OS, Microsoft Excel™ version, physical, etc.):

I use MacOS. I do not have Excel installed.

This code used to work properly before a258e3d - I used git bisect to find that.

Thank you very much for your assistance.

Alex

xuri commented 6 months ago

Thanks for your issue. This issue has been fixed, please upgrade to the master branch by go get -u github.com/xuri/excelize/v2@master, and this patch will be released in the next version.

alexbrainman-anz commented 6 months ago

This issue has been fixed, please upgrade to the master branch by go get -u github.com/xuri/excelize/v2@master, and this patch will be released in the next version.

Thank you very much for fixing the bug so quickly.

Excellent job.

Alex