mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
add fixedpoint
This commit is contained in:
parent
3ce49471d8
commit
5619deffb2
45
bbgo/fixedpoint/convert.go
Normal file
45
bbgo/fixedpoint/convert.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
package fixedpoint
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const DefaultPrecision = 8
|
||||
|
||||
const DefaultPow = 1e8
|
||||
|
||||
type Value int64
|
||||
|
||||
func (v Value) Float64() float64 {
|
||||
return float64(v) / DefaultPow
|
||||
}
|
||||
|
||||
func (v Value) Mul(v2 Value) Value {
|
||||
return NewFromFloat(v.Float64() * v2.Float64())
|
||||
}
|
||||
|
||||
func (v Value) Div(v2 Value) Value {
|
||||
return NewFromFloat(v.Float64() / v2.Float64())
|
||||
}
|
||||
|
||||
func (v Value) Sub(v2 Value) Value {
|
||||
return Value(int64(v) - int64(v2))
|
||||
}
|
||||
|
||||
func (v Value) Add(v2 Value) Value {
|
||||
return Value(int64(v) + int64(v2))
|
||||
}
|
||||
|
||||
func NewFromString(input string) (Value, error) {
|
||||
v, err := strconv.ParseFloat(input, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return NewFromFloat(v), nil
|
||||
}
|
||||
|
||||
func NewFromFloat(val float64) Value {
|
||||
return Value(int64(math.Round(val * DefaultPow)))
|
||||
}
|
Loading…
Reference in New Issue
Block a user