github.com/shakinm/xlsReader@v0.9.12/xls/structure/RKNum.go (about)

     1  package structure
     2  
     3  import (
     4  	"github.com/shakinm/xlsReader/helpers"
     5  	"math"
     6  	"strconv"
     7  )
     8  
     9  type RKNum [4]byte
    10  
    11  func (r *RKNum) number() (intNum int64, floatNum float64, isFloat bool) {
    12  	rk := helpers.BytesToUint32(r[:])
    13  
    14  	val := uint64(rk >> 2)
    15  	rkType := uint(rk << 30 >> 30)
    16  
    17  	var fn float64
    18  	switch rkType {
    19  	case 0:
    20  		fn = math.Float64frombits(uint64(rk&0xfffffffc) << 32)
    21  		isFloat = true
    22  	case 1:
    23  
    24  		fn = math.Float64frombits(uint64(rk&0xfffffffc)<<32) / 100
    25  		isFloat = true
    26  	case 3:
    27  		fn = float64(val) / 100
    28  		isFloat = true
    29  	}
    30  
    31  	return int64(val), float64(fn), isFloat
    32  }
    33  
    34  func (r *RKNum) GetFloat() (fn float64) {
    35  	i, f, isFloat := r.number()
    36  	if isFloat {
    37  		fn = f
    38  	} else {
    39  		fn=float64(i)
    40  	}
    41  	return fn
    42  }
    43  
    44  func (r *RKNum) GetInt64() (in int64) {
    45  	i, _, isFloat := r.number()
    46  	if !isFloat {
    47  		in = i
    48  	}
    49  	return in
    50  }
    51  
    52  func (r *RKNum) GetString() (s string) {
    53  	i, f, isFloat := r.number()
    54  	if isFloat {
    55  		return strconv.FormatFloat(f, 'f', -1, 64)
    56  	}
    57  	return strconv.FormatInt(i, 10)
    58  }