From b2c12d784e94bfb14da800962c769f5a0f6f783e Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 20 Mar 2019 22:41:46 -0500 Subject: SetCellFloat for floats with specific precision (#361) This allows the user to set a floating point value into a cell with a specific number of places after the decimal. Closes #357 --- cell_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'cell_test.go') diff --git a/cell_test.go b/cell_test.go index d388c7f..ba326d9 100644 --- a/cell_test.go +++ b/cell_test.go @@ -1,6 +1,7 @@ package excelize import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -39,3 +40,34 @@ func TestCheckCellInArea(t *testing.T) { checkCellInArea("AA0", "Z0:AB1") }) } + +func TestSetCellFloat(t *testing.T) { + sheet := "Sheet1" + t.Run("with no decimal", func(t *testing.T) { + f := NewFile() + f.SetCellFloat(sheet, "A1", 123.0, -1, 64) + f.SetCellFloat(sheet, "A2", 123.0, 1, 64) + assert.Equal(t, "123", f.GetCellValue(sheet, "A1"), "A1 should be 123") + assert.Equal(t, "123.0", f.GetCellValue(sheet, "A2"), "A2 should be 123.0") + }) + + t.Run("with a decimal and precision limit", func(t *testing.T) { + f := NewFile() + f.SetCellFloat(sheet, "A1", 123.42, 1, 64) + assert.Equal(t, "123.4", f.GetCellValue(sheet, "A1"), "A1 should be 123.4") + }) + + t.Run("with a decimal and no limit", func(t *testing.T) { + f := NewFile() + f.SetCellFloat(sheet, "A1", 123.42, -1, 64) + assert.Equal(t, "123.42", f.GetCellValue(sheet, "A1"), "A1 should be 123.42") + }) +} + +func ExampleFile_SetCellFloat() { + f := NewFile() + var x float64 = 3.14159265 + f.SetCellFloat("Sheet1", "A1", x, 2, 64) + fmt.Println(f.GetCellValue("Sheet1", "A1")) + // Output: 3.14 +} -- cgit v1.2.1