Skip to content

Commit

Permalink
SetCellFloat for floats with specific precision (qax-os#361)
Browse files Browse the repository at this point in the history
This allows the user to set a floating point value into a
cell with a specific number of places after the decimal.

Closes qax-os#357
  • Loading branch information
mlh758 authored and xuri committed Mar 21, 2019
1 parent 40ea8eb commit b2c12d7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
19 changes: 17 additions & 2 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ func (f *File) SetCellValue(sheet, axis string, value interface{}) {
case uint64:
f.SetCellInt(sheet, axis, int(v))
case float32:
f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(v), 'f', -1, 32))
f.SetCellFloat(sheet, axis, float64(v), -1, 32)
case float64:
f.SetCellDefault(sheet, axis, strconv.FormatFloat(v, 'f', -1, 64))
f.SetCellFloat(sheet, axis, v, -1, 64)
case string:
f.SetCellStr(sheet, axis, v)
case []byte:
Expand Down Expand Up @@ -142,6 +142,21 @@ func (f *File) SetCellBool(sheet, axis string, value bool) {
}
}

// SetCellFloat sets a floating point value into a cell. The prec parameter
// specifies how many places after the decimal will be shown while -1
// is a special value that will use as many decimal places as necessary to
// represent the number. bitSize is 32 or 64 depending on if a float32 or float64
// was originally used for the value
// var x float32 = 1.325
// f.SetCellFloat("Sheet1", "A1", float64(x), 2, 32)
func (f *File) SetCellFloat(sheet, axis string, value float64, prec, bitSize int) {
xlsx := f.workSheetReader(sheet)
cellData, col, _ := f.prepareCell(xlsx, sheet, axis)
cellData.S = f.prepareCellStyle(xlsx, col, cellData.S)
cellData.T = ""
cellData.V = strconv.FormatFloat(value, 'f', prec, 64)
}

// SetCellStr provides a function to set string type value of a cell. Total
// number of characters that a cell can contain 32767 characters.
func (f *File) SetCellStr(sheet, axis, value string) {
Expand Down
32 changes: 32 additions & 0 deletions cell_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package excelize

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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
}

0 comments on commit b2c12d7

Please sign in to comment.