20
0

为浮点数引入默认位数配置

This commit is contained in:
chen.s.g 2019-12-14 09:50:06 +08:00
parent 4b12dad937
commit e9e7409707
3 changed files with 37 additions and 30 deletions

6
col.go
View File

@ -100,7 +100,7 @@ func (rk RK) float() float64 {
func (rk RK) String(wb *WorkBook) string { func (rk RK) String(wb *WorkBook) string {
i, f, isFloat := rk.number() i, f, isFloat := rk.number()
if isFloat { if isFloat {
return strconv.FormatFloat(f, 'f', -1, 64) return strconv.FormatFloat(f, 'f', wb.defaultFloatBit, 64)
} }
return strconv.FormatInt(i, 10) return strconv.FormatInt(i, 10)
@ -188,7 +188,7 @@ func (c *NumberCol) String(wb *WorkBook) []string {
return []string{v} 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 { type FormulaColHeader struct {
@ -353,7 +353,7 @@ func (c *FormulaCol) parse(wb *WorkBook, ref bool) {
} else { } else {
c.value, flag = wb.Format(c.Header.IndexXf, c.Header.Value()) c.value, flag = wb.Format(c.Header.IndexXf, c.Header.Value())
if !flag { if !flag {
c.value = strconv.FormatFloat(c.Header.Value(), 'f', -1, 64) c.value = strconv.FormatFloat(c.Header.Value(), 'f', wb.defaultFloatBit, 64)
} }
} }
} }

View File

@ -78,7 +78,7 @@ type Format struct {
} }
// Prepare format meta data // Prepare format meta data
func (f *Format) Prepare() { func (f *Format) Prepare(wb *WorkBook) {
var regexColor = regexp.MustCompile("^\\[[a-zA-Z]+\\]") var regexColor = regexp.MustCompile("^\\[[a-zA-Z]+\\]")
var regexSharp = regexp.MustCompile("^\\d+\\.?\\d?#+") var regexSharp = regexp.MustCompile("^\\d+\\.?\\d?#+")
var regexFraction = regexp.MustCompile("#\\,?#*") var regexFraction = regexp.MustCompile("#\\,?#*")
@ -133,11 +133,15 @@ func (f *Format) Prepare() {
if f.bts > 0 { if f.bts > 0 {
f.bts = f.bts - 1 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 f.bts = -1
} else if t := strings.Index(f.Raw[0], "@"); -1 != t { } else if t := strings.Index(f.Raw[0], "@"); -1 != t {
f.bts = -1 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 { switch f.vType {
case TYPE_NUMERIC: 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) ret = strconv.FormatFloat(v, 'f', f.bts, 64)
case TYPE_CURRENCY: case TYPE_CURRENCY:
ret = strconv.FormatFloat(v, 'f', f.bts, 64) ret = strconv.FormatFloat(v, 'f', f.bts, 64)

View File

@ -26,12 +26,14 @@ type WorkBook struct {
continue_rich uint16 continue_rich uint16
continue_apsb uint32 continue_apsb uint32
dateMode uint16 dateMode uint16
defaultFloatBit int
} }
//newWorkBookFromOle2 read workbook from ole2 file //newWorkBookFromOle2 read workbook from ole2 file
func newWorkBookFromOle2(rs io.ReadSeeker) *WorkBook { func newWorkBookFromOle2(rs io.ReadSeeker) *WorkBook {
var wb = &WorkBook{ var wb = &WorkBook{
rs: rs, rs: rs,
defaultFloatBit: -1,
ref: new(extSheetRef), ref: new(extSheetRef),
sheets: make([]*WorkSheet, 0), sheets: make([]*WorkSheet, 0),
Formats: make(map[uint16]*Format), Formats: make(map[uint16]*Format),
@ -48,6 +50,11 @@ func (w *WorkBook) SetDebug(debug bool) {
w.Debug = debug w.Debug = debug
} }
// SetFloatBit 设置小数默认保留位数,默认值 -1 不限制
func (w *WorkBook) SetFloatBit(n int) {
w.defaultFloatBit = n
}
func (w *WorkBook) parse(buf io.ReadSeeker) { func (w *WorkBook) parse(buf io.ReadSeeker) {
b := new(bof) b := new(bof)
bp := new(bof) bp := new(bof)
@ -194,7 +201,7 @@ func (w *WorkBook) prepare() {
} }
} }
for _, v := range w.Formats { for _, v := range w.Formats {
v.Prepare() v.Prepare(w)
} }
} }