为浮点数引入默认位数配置
This commit is contained in:
parent
4b12dad937
commit
e9e7409707
6
col.go
6
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
12
format.go
12
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)
|
||||
|
@ -26,12 +26,14 @@ type WorkBook struct {
|
||||
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,
|
||||
defaultFloatBit: -1,
|
||||
ref: new(extSheetRef),
|
||||
sheets: make([]*WorkSheet, 0),
|
||||
Formats: make(map[uint16]*Format),
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user