Use bitSize for float32 type numbers conversion, relate PR #361

This commit is contained in:
xuri 2019-03-21 14:09:25 +08:00
parent b2c12d784e
commit 70b1a29165
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 12 additions and 10 deletions

16
cell.go
View File

@ -143,18 +143,20 @@ func (f *File) SetCellBool(sheet, axis string, value bool) {
} }
// SetCellFloat sets a floating point value into a cell. The prec parameter // SetCellFloat sets a floating point value into a cell. The prec parameter
// specifies how many places after the decimal will be shown while -1 // specifies how many places after the decimal will be shown while -1 is a
// is a special value that will use as many decimal places as necessary to // 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 // represent the number. bitSize is 32 or 64 depending on if a float32 or
// was originally used for the value // float64 was originally used for the value. For Example:
// var x float32 = 1.325 //
// f.SetCellFloat("Sheet1", "A1", float64(x), 2, 32) // 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) { func (f *File) SetCellFloat(sheet, axis string, value float64, prec, bitSize int) {
xlsx := f.workSheetReader(sheet) xlsx := f.workSheetReader(sheet)
cellData, col, _ := f.prepareCell(xlsx, sheet, axis) cellData, col, _ := f.prepareCell(xlsx, sheet, axis)
cellData.S = f.prepareCellStyle(xlsx, col, cellData.S) cellData.S = f.prepareCellStyle(xlsx, col, cellData.S)
cellData.T = "" cellData.T = ""
cellData.V = strconv.FormatFloat(value, 'f', prec, 64) cellData.V = strconv.FormatFloat(value, 'f', prec, bitSize)
} }
// SetCellStr provides a function to set string type value of a cell. Total // SetCellStr provides a function to set string type value of a cell. Total

View File

@ -66,7 +66,7 @@ func TestSetCellFloat(t *testing.T) {
func ExampleFile_SetCellFloat() { func ExampleFile_SetCellFloat() {
f := NewFile() f := NewFile()
var x float64 = 3.14159265 var x = 3.14159265
f.SetCellFloat("Sheet1", "A1", x, 2, 64) f.SetCellFloat("Sheet1", "A1", x, 2, 64)
fmt.Println(f.GetCellValue("Sheet1", "A1")) fmt.Println(f.GetCellValue("Sheet1", "A1"))
// Output: 3.14 // Output: 3.14

View File

@ -38,14 +38,14 @@ func TestTimeToExcelTime(t *testing.T) {
} }
func TestTimeToExcelTime_Timezone(t *testing.T) { func TestTimeToExcelTime_Timezone(t *testing.T) {
msk, err := time.LoadLocation("Europe/Moscow") location, err := time.LoadLocation("America/Los_Angeles")
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
t.FailNow() t.FailNow()
} }
for i, test := range trueExpectedDateList { for i, test := range trueExpectedDateList {
t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) { t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
assert.Panics(t, func() { assert.Panics(t, func() {
timeToExcelTime(test.GoValue.In(msk)) timeToExcelTime(test.GoValue.In(location))
}, "Time: %s", test.GoValue.String()) }, "Time: %s", test.GoValue.String())
}) })
} }