diff --git a/col.go b/col.go index 7b441c5..20dc111 100644 --- a/col.go +++ b/col.go @@ -100,7 +100,7 @@ func (rk RK) float() float64 { func (rk RK) String(wb *WorkBook) string { i, f, isFloat := rk.number() if isFloat { - return strconv.FormatFloat(f, 'f', -1, 64) + return strconv.FormatFloat(f, 'f', wb.defaultFloatBit, 64) } return strconv.FormatInt(i, 10) @@ -188,7 +188,7 @@ func (c *NumberCol) String(wb *WorkBook) []string { return []string{v} } - return []string{strconv.FormatFloat(c.Float, 'f', -1, 64)} + return []string{strconv.FormatFloat(c.Float, 'f', wb.defaultFloatBit, 64)} } type FormulaColHeader struct { @@ -353,7 +353,7 @@ func (c *FormulaCol) parse(wb *WorkBook, ref bool) { } else { c.value, flag = wb.Format(c.Header.IndexXf, c.Header.Value()) if !flag { - c.value = strconv.FormatFloat(c.Header.Value(), 'f', -1, 64) + c.value = strconv.FormatFloat(c.Header.Value(), 'f', wb.defaultFloatBit, 64) } } } diff --git a/format.go b/format.go index c0405db..b98ba3a 100644 --- a/format.go +++ b/format.go @@ -78,7 +78,7 @@ type Format struct { } // Prepare format meta data -func (f *Format) Prepare() { +func (f *Format) Prepare(wb *WorkBook) { var regexColor = regexp.MustCompile("^\\[[a-zA-Z]+\\]") var regexSharp = regexp.MustCompile("^\\d+\\.?\\d?#+") var regexFraction = regexp.MustCompile("#\\,?#*") @@ -133,11 +133,15 @@ func (f *Format) Prepare() { if f.bts > 0 { f.bts = f.bts - 1 } - } else if t := strings.Index(f.Raw[0], "General"); -1 != t { + } else if -1 != strings.Index(f.Raw[0], "General") || -1 != strings.Index(f.Raw[0], "general") { f.bts = -1 } else if t := strings.Index(f.Raw[0], "@"); -1 != t { f.bts = -1 } + + if -1 == f.bts { + f.bts = wb.defaultFloatBit + } } } @@ -148,10 +152,6 @@ func (f *Format) String(v float64) string { switch f.vType { case TYPE_NUMERIC: - if 0 == f.bts && nil != f.Raw && "general" == f.Raw[0] { - f.bts = -1 - } - ret = strconv.FormatFloat(v, 'f', f.bts, 64) case TYPE_CURRENCY: ret = strconv.FormatFloat(v, 'f', f.bts, 64) diff --git a/workbook.go b/workbook.go index cd16607..cb31c8c 100644 --- a/workbook.go +++ b/workbook.go @@ -10,31 +10,33 @@ import ( //WorkBook excel work book type WorkBook struct { - Debug bool - Is5ver bool - Type uint16 - Codepage uint16 - Xfs []XF - Fonts []Font - Formats map[uint16]*Format - sheets []*WorkSheet - Author string - rs io.ReadSeeker - sst []string - ref *extSheetRef - continue_utf16 uint16 - continue_rich uint16 - continue_apsb uint32 - dateMode uint16 + Debug bool + Is5ver bool + Type uint16 + Codepage uint16 + Xfs []XF + Fonts []Font + Formats map[uint16]*Format + sheets []*WorkSheet + Author string + rs io.ReadSeeker + sst []string + ref *extSheetRef + continue_utf16 uint16 + continue_rich uint16 + continue_apsb uint32 + dateMode uint16 + defaultFloatBit int } //newWorkBookFromOle2 read workbook from ole2 file func newWorkBookFromOle2(rs io.ReadSeeker) *WorkBook { var wb = &WorkBook{ - rs: rs, - ref: new(extSheetRef), - sheets: make([]*WorkSheet, 0), - Formats: make(map[uint16]*Format), + rs: rs, + defaultFloatBit: -1, + ref: new(extSheetRef), + sheets: make([]*WorkSheet, 0), + Formats: make(map[uint16]*Format), } wb.parse(rs) @@ -48,6 +50,11 @@ func (w *WorkBook) SetDebug(debug bool) { w.Debug = debug } +// SetFloatBit 设置小数默认保留位数,默认值 -1 不限制 +func (w *WorkBook) SetFloatBit(n int) { + w.defaultFloatBit = n +} + func (w *WorkBook) parse(buf io.ReadSeeker) { b := new(bof) bp := new(bof) @@ -194,7 +201,7 @@ func (w *WorkBook) prepare() { } } for _, v := range w.Formats { - v.Prepare() + v.Prepare(w) } }