diff --git a/.gitignore b/.gitignore index 547ecb9..c7afc7c 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ *.out c.out coverage.html +.mise.toml # Test binary, built with `go test -c` *.test @@ -43,4 +44,4 @@ test/test .DS_Store # Editor files -.idea \ No newline at end of file +.idea diff --git a/rounding_test.go b/rounding_test.go new file mode 100644 index 0000000..02acc27 --- /dev/null +++ b/rounding_test.go @@ -0,0 +1,46 @@ +package xlsxreader + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func getColCount(f *XlsxFileCloser) int { + row := <-f.ReadRows(f.Sheets[0]) + if row.Error != nil { + return 0 + } + return asIndex(row.Cells[len(row.Cells)-1].Column) + 1 +} + +func TestFloatingPointRounding(t *testing.T) { + + t.Run("rounding", func(t *testing.T) { + xl, err := OpenFile("./test/test-rounding.xlsx") + // Create an instance of the reader by opening a target file + var target string + // Ensure the file reader is closed once utilised + defer xl.Close() + numColumns := getColCount(xl) + for row := range xl.ReadRows(xl.Sheets[0]) { + if row.Error != nil { + continue + } + csvRow := make([]string, numColumns) + for _, curCell := range row.Cells { + colIndex := asIndex(curCell.Column) + if curCell.Column == "L" && curCell.Row == 2 { + target = curCell.Value + } + if colIndex < numColumns { + csvRow[colIndex] = curCell.Value + } + } + + } + + require.NoError(t, err) + require.Equal(t, "4.4", target) + }) +} diff --git a/rows.go b/rows.go index c9ecde0..82bf406 100644 --- a/rows.go +++ b/rows.go @@ -232,7 +232,12 @@ func (x *XlsxFile) getCellValue(r rawCell) (string, error) { return formattedDate, nil } - return *r.Value, nil + v, err := strconv.ParseFloat(*r.Value, 64) + if err != nil { + return *r.Value, nil + } + new_val := strconv.FormatFloat(v, 'f', -1, 64) + return new_val, nil } func (x *XlsxFile) getCellType(r rawCell) CellType { diff --git a/test/test-rounding.xlsx b/test/test-rounding.xlsx new file mode 100644 index 0000000..ffaeb26 Binary files /dev/null and b/test/test-rounding.xlsx differ