/
number.go
41 lines (33 loc) · 1.06 KB
/
number.go
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
37
38
39
40
41
package transform
import (
"fmt"
"strconv"
iconfig "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/internal/errors"
)
type numberMathConfig struct {
Object iconfig.Object `json:"object"`
}
func (c *numberMathConfig) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func (c *numberMathConfig) Validate() error {
if c.Object.SourceKey == "" && c.Object.TargetKey != "" {
return fmt.Errorf("object_source_key: %v", errors.ErrMissingRequiredOption)
}
if c.Object.SourceKey != "" && c.Object.TargetKey == "" {
return fmt.Errorf("object_target_key: %v", errors.ErrMissingRequiredOption)
}
return nil
}
// numberFloat64ToString addresses multiple issues with performing math
// operations on floats:
//
// - Converts the float to a string without scientific notation: 1.1e+9 -> 1100000000
//
// - Truncates the float to remove trailing zeros: 1.100000000 -> 1.1
//
// - Removes the decimal point if it is a whole number: 1.0 -> 1
func numberFloat64ToString(v float64) string {
return strconv.FormatFloat(v, 'f', -1, 64)
}