Skip to content

Commit 96b7447

Browse files
committed
Humaner.FormatNumber(): return early in the "unusual" case
Also, convert a chained `if` statement into a `switch`.
1 parent 59f84fa commit 96b7447

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

counts/human.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,22 @@ func (h *Humaner) FormatNumber(n uint64, unit string) (string, string) {
7474

7575
if prefix.Multiplier == 1 {
7676
return fmt.Sprintf("%d", n), unit
77-
} else {
78-
mantissa := float64(n) / float64(prefix.Multiplier)
79-
var format string
77+
}
8078

81-
if wholePart >= 100 {
82-
// `mantissa` can actually be up to 1023.999.
83-
format = "%.0f"
84-
} else if wholePart >= 10 {
85-
format = "%.1f"
86-
} else {
87-
format = "%.2f"
88-
}
89-
return fmt.Sprintf(format, mantissa), prefix.Name + unit
79+
mantissa := float64(n) / float64(prefix.Multiplier)
80+
var format string
81+
82+
switch {
83+
case wholePart >= 100:
84+
// `mantissa` can actually be up to 1023.999.
85+
format = "%.0f"
86+
case wholePart >= 10:
87+
format = "%.1f"
88+
default:
89+
format = "%.2f"
9090
}
91+
92+
return fmt.Sprintf(format, mantissa), prefix.Name + unit
9193
}
9294

9395
// Format formats values, aligned, in `len(unit) + 10` or fewer

0 commit comments

Comments
 (0)