This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
number.go
158 lines (129 loc) · 3.05 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package human
import (
"encoding"
"encoding/json"
"flag"
"fmt"
"io"
"math"
"strconv"
"strings"
yaml "gopkg.in/yaml.v3"
)
// Number is similar to Count, but supports values with separators for
// readability purposes.
//
// The type supports parsing and formatting values likes:
//
// 123
// 1.5
// 2,000,000
// ...
type Number float64
func ParseNumber(s string) (Number, error) {
r := strings.ReplaceAll(s, ",", "")
f, err := strconv.ParseFloat(r, 64)
if err != nil {
return 0, fmt.Errorf("malformed number: %s: %w", s, err)
}
return Number(f), nil
}
func (n Number) String() string {
if n == 0 {
return "0"
}
if n < 0 {
return "-" + (-n).String()
}
if n <= 1e-3 || n >= 1e12 {
return strconv.FormatFloat(float64(n), 'g', -1, 64)
}
i, d := math.Modf(float64(n))
parts := make([]string, 0, 4)
for u := uint64(i); u > 0; u /= 1000 {
parts = append(parts, strconv.FormatUint(u%1000, 10))
}
for i, j := 0, len(parts)-1; i < j; {
parts[i], parts[j] = parts[j], parts[i]
i++
j--
}
r := strings.Join(parts, ",")
if d != 0 {
r += "."
r += suffix('0').trim(strconv.FormatUint(uint64(math.Round(d*1000)), 10))
}
return r
}
func (n Number) GoString() string {
return fmt.Sprintf("human.Number(%v)", float64(n))
}
// Format satisfies the fmt.Formatter interface.
//
// The method supports the following formatting verbs:
//
// e base 10, separator-free, scientific notation
// f base 10, separator-free, decimal notation
// g base 10, separator-free, act like 'e' or 'f' depending on scale
// s base 10, with separators (same as calling String)
// v same as the 's' format, unless '#' is set to print the go value
func (n Number) Format(w fmt.State, v rune) {
_, _ = io.WriteString(w, n.format(w, v))
}
func (n Number) format(w fmt.State, v rune) string {
switch v {
case 'e', 'f', 'g':
return strconv.FormatFloat(float64(n), byte(v), -1, 64)
case 's':
return n.String()
case 'v':
if w.Flag('#') {
return n.GoString()
}
return n.format(w, 's')
default:
return printError(v, n, float64(n))
}
}
func (n Number) Get() any {
return float64(n)
}
func (n *Number) Set(s string) error {
p, err := ParseNumber(s)
if err != nil {
return err
}
*n = p
return nil
}
func (n Number) MarshalJSON() ([]byte, error) {
return json.Marshal(float64(n))
}
func (n *Number) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, (*float64)(n))
}
func (n Number) MarshalYAML() (any, error) {
return float64(n), nil
}
func (n *Number) UnmarshalYAML(y *yaml.Node) error {
return y.Decode((*float64)(n))
}
func (n Number) MarshalText() ([]byte, error) {
return []byte(n.String()), nil
}
func (n *Number) UnmarshalText(b []byte) error {
return n.Set(string(b))
}
var (
_ fmt.Formatter = Number(0)
_ fmt.GoStringer = Number(0)
_ fmt.Stringer = Number(0)
_ json.Marshaler = Number(0)
_ json.Unmarshaler = (*Number)(nil)
_ yaml.Marshaler = Number(0)
_ yaml.Unmarshaler = (*Number)(nil)
_ encoding.TextMarshaler = Number(0)
_ encoding.TextUnmarshaler = (*Number)(nil)
_ flag.Getter = (*Number)(nil)
_ flag.Value = (*Number)(nil)
)