Browse Source

Fixed: SetFloatWithFormat can't write the value with proper precision.

Hugh Gao 10 years ago
parent
commit
7323f9b5f6
2 changed files with 10 additions and 2 deletions
  1. 1 2
      cell.go
  2. 9 0
      cell_test.go

+ 1 - 2
cell.go

@@ -88,8 +88,7 @@ func (c *Cell) SetFloat(n float64) {
 // SetFloatWithFormat sets the value of a cell to a float and applies
 // formatting to the cell.
 func (c *Cell) SetFloatWithFormat(n float64, format string) {
-	// tmp value. final value is formatted by FormattedValue() method
-	c.Value = fmt.Sprintf("%e", n)
+	c.Value = strconv.FormatFloat(n, 'e', -1, 64)
 	c.numFmt = format
 	c.formula = ""
 	c.cellType = CellTypeNumeric

+ 9 - 0
cell_test.go

@@ -93,6 +93,15 @@ func (s *CellSuite) TestGetStyleWithBorders(c *C) {
 	c.Assert(xBorder.Bottom.Style, Equals, "thin")
 }
 
+// We can return a string representation of the formatted data
+func (l *CellSuite) TestSetFloatWithFormat(c *C) {
+	cell := Cell{}
+	cell.SetFloatWithFormat(37947.75334343, "yyyy/mm/dd")
+	c.Assert(cell.Value, Equals, "3.794775334343e+04")
+	c.Assert(cell.numFmt, Equals, "yyyy/mm/dd")
+	c.Assert(cell.Type(), Equals, CellTypeNumeric)
+}
+
 // We can return a string representation of the formatted data
 func (l *CellSuite) TestFormattedValue(c *C) {
 	cell := Cell{Value: "37947.7500001"}