Skip to content

Commit 669db16

Browse files
committed
fix:修复显示负数值的问题
1 parent a2e68c9 commit 669db16

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

sp/sp.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ func NearbySpeedTest() {
260260
})
261261
if err == nil {
262262
fmt.Print(formatString("Speedtest.net", 16))
263-
fmt.Print(formatString(fmt.Sprintf("%-8s", fmt.Sprintf("%.2f", NearbyServer.ULSpeed.Mbps())+" Mbps"), 16))
264-
fmt.Print(formatString(fmt.Sprintf("%-8s", fmt.Sprintf("%.2f", NearbyServer.DLSpeed.Mbps())+" Mbps"), 16))
263+
fmt.Print(formatString(formatMbps(NearbyServer.ULSpeed.Mbps()), 16))
264+
fmt.Print(formatString(formatMbps(NearbyServer.DLSpeed.Mbps()), 16))
265265
fmt.Print(formatString(NearbyServer.Latency.String(), 16))
266266
fmt.Print(formatString(PacketLoss, 16))
267267
fmt.Println()
@@ -371,8 +371,8 @@ func CustomSpeedTest(url, byWhat string, num int, language string) {
371371
name = strings.ReplaceAll(name, "法兰克福", "Frankfurt")
372372
fmt.Print(formatString(name, 16))
373373
}
374-
fmt.Print(formatString(fmt.Sprintf("%-8s", fmt.Sprintf("%.2f", server.ULSpeed.Mbps())+" Mbps"), 16))
375-
fmt.Print(formatString(fmt.Sprintf("%-8s", fmt.Sprintf("%.2f", server.DLSpeed.Mbps())+" Mbps"), 16))
374+
fmt.Print(formatString(formatMbps(server.ULSpeed.Mbps()), 16))
375+
fmt.Print(formatString(formatMbps(server.DLSpeed.Mbps()), 16))
376376
fmt.Print(formatString(server.Latency.String(), 16))
377377
fmt.Print(formatString(PacketLoss, 16))
378378
fmt.Println()

sp/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/csv"
66
"fmt"
77
"io"
8+
"math"
89
"strings"
910
"time"
1011
"unicode/utf8"
@@ -272,6 +273,13 @@ func formatString(s string, width int) string {
272273
return s
273274
}
274275

276+
func formatMbps(value float64) string {
277+
if math.IsNaN(value) || math.IsInf(value, 0) || value < 0 {
278+
value = 0
279+
}
280+
return fmt.Sprintf("%.2f Mbps", value)
281+
}
282+
275283
func ShowHead(language string) {
276284
headers1 := []string{"位置", "上传速度", "下载速度", "延迟", "丢包率"}
277285
headers2 := []string{"Location", "Upload Speed", "Download Speed", "Latency", "PacketLoss"}

sp/utils_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package sp
2+
3+
import (
4+
"math"
5+
"testing"
6+
)
7+
8+
func TestFormatMbps(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
value float64
12+
want string
13+
}{
14+
{name: "negative", value: -0.0001, want: "0.00 Mbps"},
15+
{name: "nan", value: math.NaN(), want: "0.00 Mbps"},
16+
{name: "positive", value: 293.075, want: "293.08 Mbps"},
17+
}
18+
19+
for _, tc := range tests {
20+
t.Run(tc.name, func(t *testing.T) {
21+
got := formatMbps(tc.value)
22+
if got != tc.want {
23+
t.Fatalf("formatMbps(%v) = %q, want %q", tc.value, got, tc.want)
24+
}
25+
})
26+
}
27+
}

0 commit comments

Comments
 (0)