Standardize with ollama.com

This commit is contained in:
Roy Han 2024-06-24 10:53:15 -07:00
parent af370ac178
commit f93cdfdfae
2 changed files with 33 additions and 25 deletions

View File

@ -2,32 +2,41 @@ package format
import ( import (
"fmt" "fmt"
"math"
)
const (
Thousand = 1000
Million = Thousand * 1000
Billion = Million * 1000
) )
func HumanNumber(b uint64) string { func HumanNumber(b uint64) string {
const (
Thousand = 1000
Million = Thousand * 1000
Billion = Million * 1000
Trillion = Billion * 1000
)
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("%.2fB", number) // two decimals 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)
}
}

View File

@ -11,15 +11,14 @@ func TestHumanNumber(t *testing.T) {
} }
testCases := []testCase{ testCases := []testCase{
{0, "0"}, {26000000, "26.0M"},
{1000000, "1M"}, {26000000000, "26.0B"},
{125000000, "125M"}, {1000, "1.00K"},
{500500000, "500.50M"}, {1000000, "1.00M"},
{500550000, "500.55M"}, {1000000000, "1.00B"},
{1000000000, "1B"}, {1000000000000, "1.00T"},
{2800000000, "2.80B"}, {100, "100"},
{2850000000, "2.85B"}, {206000000, "206M"},
{1000000000000, "1000B"},
} }
for _, tc := range testCases { for _, tc := range testCases {