Browse Source

de-dupe code

Ryan Hollis 8 years ago
parent
commit
d3bb6c4101
1 changed files with 14 additions and 18 deletions
  1. 14 18
      cell.go

+ 14 - 18
cell.go

@@ -210,33 +210,30 @@ func (c *Cell) Int64() (int64, error) {
 // to display values when the storage type is Number and the format type is General. It is not 100% identical to the
 // spec but is as close as you can get using the built in Go formatting tools.
 func (c *Cell) GeneralNumeric() (string, error) {
-	f, err := strconv.ParseFloat(c.Value, 64)
-	if err != nil {
-		return c.Value, err
-	}
-	absF := math.Abs(f)
-	// When using General format, numbers that are less than 1e-9 (0.000000001) and greater than or equal to
-	// 1e11 (100,000,000,000) should be shown in scientific notation.
-	// Numbers less than the number after zero, are assumed to be zero.
-	if (absF >= math.SmallestNonzeroFloat64 && absF < minNonScientificNumber) || absF >= maxNonScientificNumber {
-		return strconv.FormatFloat(f, 'E', -1, 64), nil
-	}
-	// This format (fmt="f", prec=-1) will prevent padding with zeros and will never switch to scientific notation.
-	// However, it will show more than 11 characters for very precise numbers, and this cannot be changed.
-	// You could also use fmt="g", prec=11, which doesn't pad with zeros and allows the correct precision,
-	// but it will use scientific notation on numbers less than 1e-4. That value is hardcoded in Go and cannot be
-	// configured or disabled.
-	return strconv.FormatFloat(f, 'f', -1, 64), nil
+	return c.generalNumericScientific(true)
 }
 
 // GeneralNumericWithoutScientific returns numbers that are always formatted as numbers, but it does not follow
 // the rules for when XLSX should switch to scientific notation, since sometimes scientific notation is not desired,
 // even if that is how the document is supposed to be formatted.
 func (c *Cell) GeneralNumericWithoutScientific() (string, error) {
+	return c.generalNumericScientific(false)
+}
+
+func (c *Cell) generalNumericScientific(allowScientific bool) (string, error) {
 	f, err := strconv.ParseFloat(c.Value, 64)
 	if err != nil {
 		return c.Value, err
 	}
+	if allowScientific {
+		absF := math.Abs(f)
+		// When using General format, numbers that are less than 1e-9 (0.000000001) and greater than or equal to
+		// 1e11 (100,000,000,000) should be shown in scientific notation.
+		// Numbers less than the number after zero, are assumed to be zero.
+		if (absF >= math.SmallestNonzeroFloat64 && absF < minNonScientificNumber) || absF >= maxNonScientificNumber {
+			return strconv.FormatFloat(f, 'E', -1, 64), nil
+		}
+	}
 	// This format (fmt="f", prec=-1) will prevent padding with zeros and will never switch to scientific notation.
 	// However, it will show more than 11 characters for very precise numbers, and this cannot be changed.
 	// You could also use fmt="g", prec=11, which doesn't pad with zeros and allows the correct precision,
@@ -244,7 +241,6 @@ func (c *Cell) GeneralNumericWithoutScientific() (string, error) {
 	// configured or disabled.
 	return strconv.FormatFloat(f, 'f', -1, 64), nil
 }
-
 // SetInt sets a cell's value to an integer.
 func (c *Cell) SetInt(n int) {
 	c.SetValue(n)