Compare commits
3 Commits
main
...
jyan/forma
Author | SHA1 | Date | |
---|---|---|---|
|
5dc5a295bf | ||
|
e21e6b2a33 | ||
|
a240ea3367 |
@ -2,32 +2,41 @@ package format
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Thousand = 1000
|
Thousand = 1000
|
||||||
Million = Thousand * 1000
|
Million = Thousand * 1000
|
||||||
Billion = Million * 1000
|
Billion = Million * 1000
|
||||||
|
Trillion = Billion * 1000
|
||||||
)
|
)
|
||||||
|
|
||||||
func HumanNumber(b uint64) string {
|
func HumanNumber(b uint64) string {
|
||||||
switch {
|
switch {
|
||||||
|
case b >= Trillion:
|
||||||
|
number := float64(b) / Trillion
|
||||||
|
return fmt.Sprintf("%sT", DecimalPlace(number))
|
||||||
case b >= Billion:
|
case b >= Billion:
|
||||||
number := float64(b) / Billion
|
number := float64(b) / Billion
|
||||||
if number == math.Floor(number) {
|
return fmt.Sprintf("%sB", DecimalPlace(number))
|
||||||
return fmt.Sprintf("%.0fB", number) // no decimals if whole number
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%.1fB", number) // one decimal if not a whole number
|
|
||||||
case b >= Million:
|
case b >= Million:
|
||||||
number := float64(b) / Million
|
number := float64(b) / Million
|
||||||
if number == math.Floor(number) {
|
return fmt.Sprintf("%sM", DecimalPlace(number))
|
||||||
return fmt.Sprintf("%.0fM", number) // no decimals if whole number
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%.2fM", number) // two decimals if not a whole number
|
|
||||||
case b >= Thousand:
|
case b >= Thousand:
|
||||||
return fmt.Sprintf("%.0fK", float64(b)/Thousand)
|
number := float64(b) / Thousand
|
||||||
|
return fmt.Sprintf("%sK", DecimalPlace(number))
|
||||||
default:
|
default:
|
||||||
return fmt.Sprintf("%d", b)
|
return fmt.Sprintf("%d", b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DecimalPlace(number float64) string {
|
||||||
|
switch {
|
||||||
|
case number >= 100:
|
||||||
|
return fmt.Sprintf("%.0f", number)
|
||||||
|
case number >= 10:
|
||||||
|
return fmt.Sprintf("%.1f", number)
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("%.2f", number)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -13,14 +13,15 @@ func TestHumanNumber(t *testing.T) {
|
|||||||
|
|
||||||
testCases := []testCase{
|
testCases := []testCase{
|
||||||
{0, "0"},
|
{0, "0"},
|
||||||
{1000000, "1M"},
|
{1000000, "1.00M"},
|
||||||
{125000000, "125M"},
|
{125000000, "125M"},
|
||||||
{500500000, "500.50M"},
|
{500500000, "500M"},
|
||||||
{500550000, "500.55M"},
|
{500550000, "501M"},
|
||||||
{1000000000, "1B"},
|
{1000000000, "1.00B"},
|
||||||
{2800000000, "2.8B"},
|
{2800000000, "2.80B"},
|
||||||
{2850000000, "2.9B"},
|
{2850000000, "2.85B"},
|
||||||
{1000000000000, "1000B"},
|
{28550000000, "28.6B"},
|
||||||
|
{1000000000000, "1.00T"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user