forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_float.go
More file actions
36 lines (28 loc) · 738 Bytes
/
fix_float.go
File metadata and controls
36 lines (28 loc) · 738 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package quickfix
import (
"fmt"
"regexp"
"strconv"
)
//FIXFloat is a FIX Float Value, implements FieldValue
type FIXFloat float64
//NewFIXFloat returns an initialized FIXFloat
func NewFIXFloat(val float64) *FIXFloat {
f := FIXFloat(val)
return &f
}
func (f *FIXFloat) Read(bytes []byte) error {
val, err := strconv.ParseFloat(string(bytes), 64)
if err != nil {
return err
}
//strconv allows values like "+100.00", which is not allowed for FIX float types
if valid, _ := regexp.MatchString("^[0-9.-]+$", string(bytes)); !valid {
return fmt.Errorf("invalid value %v", string(bytes))
}
*f = FIXFloat(val)
return err
}
func (f FIXFloat) Write() []byte {
return []byte(strconv.FormatFloat(float64(f), 'f', -1, 64))
}