24 lines
597 B
Go
24 lines
597 B
Go
package probe
|
|
|
|
import "fmt"
|
|
|
|
// HumanizeBitsPerSecond formats a bit rate as kilobit, megabit, or gigabit.
|
|
func HumanizeBitsPerSecond(bitsPerSecond float64) string {
|
|
const (
|
|
kilobit = 1000
|
|
megabit = 1000 * kilobit
|
|
gigabit = 1000 * megabit
|
|
)
|
|
|
|
switch {
|
|
case bitsPerSecond >= gigabit:
|
|
return fmt.Sprintf("%.2f Gbit/s", bitsPerSecond/gigabit)
|
|
case bitsPerSecond >= megabit:
|
|
return fmt.Sprintf("%.2f Mbit/s", bitsPerSecond/megabit)
|
|
case bitsPerSecond >= kilobit:
|
|
return fmt.Sprintf("%.2f kbit/s", bitsPerSecond/kilobit)
|
|
default:
|
|
return fmt.Sprintf("%.0f bit/s", bitsPerSecond)
|
|
}
|
|
}
|