Skip to content

Commit cb5fcde

Browse files
committed
fix negative error text
1 parent e23d26a commit cb5fcde

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

value.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,8 @@ func (v Value) Format(f fmt.State, verb rune) {
1717
var scale rune
1818
divisor := Value(1024.0)
1919
sign := byte(0)
20-
21-
negative := math.Signbit(float64(v))
22-
if negative {
23-
v = Value(math.Abs(float64(v)))
24-
sign = '-'
25-
} else if f.Flag('+') {
26-
sign = '+'
27-
}
28-
2920
unit := byte('B')
21+
3022
switch verb {
3123
case 'v':
3224
// default byte formatter
@@ -39,10 +31,19 @@ func (v Value) Format(f fmt.State, verb rune) {
3931
_, _ = fmt.Fprintf(f, "%%!%c(%T=%v)", verb, v, float32(v))
4032
return
4133
}
34+
4235
if f.Flag('#') {
4336
unit = 0
4437
}
4538

39+
negative := math.Signbit(float64(v))
40+
if negative {
41+
v = Value(math.Abs(float64(v)))
42+
sign = '-'
43+
} else if f.Flag('+') {
44+
sign = '+'
45+
}
46+
4647
if v >= 1000 {
4748
for _, scale = range "kMGTPEZYRQ" {
4849
v /= divisor

value_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ func TestValue_Format(t *testing.T) {
125125
f: "%q",
126126
v: 31,
127127
},
128+
{
129+
name: "%!x(bytecount.Value=-31)",
130+
f: "%x",
131+
v: -31,
132+
},
128133
}
129134
for _, tt := range tests {
130135
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)