-
Notifications
You must be signed in to change notification settings - Fork 7
/
numbers.go
51 lines (40 loc) · 1.15 KB
/
numbers.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
42
43
44
45
46
47
48
49
50
51
package numbers
import "github.com/dustin/go-humanize"
type NumberFormatter interface {
Display(float64, string, string) string
}
type RawNumber struct{}
func (rawNum *RawNumber) Display(v float64, raw string, unit string) string {
return raw
}
type NumberWithCommas struct{}
func (rawNum *NumberWithCommas) Display(v float64, raw string, unit string) string {
return humanize.Commaf(v)
}
type NumberViewOptions struct {
Precision int
ActivateRangeMin float64
ActivateRangeMax float64
}
func NewNumberViewOptions(precision int, rangeMin float64, rangeMax float64) *NumberViewOptions {
return &NumberViewOptions{
Precision: precision,
ActivateRangeMin: rangeMin,
ActivateRangeMax: rangeMax,
}
}
func (num *NumberViewOptions) GetPrecision() int {
if num.Precision < 0 {
return 2
}
return num.Precision
}
func (num *NumberViewOptions) Display(v float64, raw string, unit string) string {
if num.IsVerySmallOrVeryLarge(v) {
return humanize.SIWithDigits(v, num.Precision, unit)
}
return raw
}
func (num *NumberViewOptions) IsVerySmallOrVeryLarge(v float64) bool {
return v <= num.ActivateRangeMin || v >= num.ActivateRangeMax
}