-
Notifications
You must be signed in to change notification settings - Fork 239
money notation #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
money notation #95
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories | ||
vendor/ | ||
|
||
# Editor settings | ||
.vscode | ||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package humanize | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func Finance(f float64) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it would make sense to take some optional So you could do things like:
etc. Could also look at the various There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good idea, I'll add the Options pattern, the options will be:
WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ping @harrisonhjones |
||
switch n := f; { | ||
case n >= 1_000_000_000_000_000: | ||
s := fmt.Sprintf("%.f", f) | ||
s = insertAt(1, '.', []rune(s[:4])) | ||
return fmt.Sprintf("$%sQ", s) | ||
|
||
case n >= 1_000_000_000_000: | ||
s := fmt.Sprintf("%.f", f) | ||
s = insertAt(1, '.', []rune(s[:4])) | ||
return fmt.Sprintf("$%sT", s) | ||
|
||
case n >= 1_000_000_000: | ||
s := fmt.Sprintf("%.f", f) | ||
s = insertAt(1, '.', []rune(s[:4])) | ||
return fmt.Sprintf("$%sB", s) | ||
|
||
case n >= 1_000_000: | ||
s := fmt.Sprintf("%.f", f) | ||
s = insertAt(1, '.', []rune(s[:4])) | ||
return fmt.Sprintf("$%sM", s) | ||
|
||
case n >= 1_000: | ||
s := fmt.Sprintf("%.f", f) | ||
s = insertAt(1, '.', []rune(s[:4])) | ||
return fmt.Sprintf("$%sK", s) | ||
|
||
case n < 1_000: | ||
return fmt.Sprintf("$%.f", f) | ||
|
||
default: | ||
return "NaN" | ||
} | ||
} | ||
|
||
func insertAt(index int, elem rune, slice []rune) string { | ||
copy(slice[index+1:], slice[index:]) | ||
slice[index] = elem | ||
return string(slice) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package humanize | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
) | ||
|
||
func TestFinance(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
arg float64 | ||
want string | ||
}{ | ||
{"Quadrillions", 2_475_260_494_216_000, "$2.47Q"}, | ||
{"Trillions", 2_475_260_494_216, "$2.47T"}, | ||
{"Billions", 2_475_260_494, "$2.47B"}, | ||
{"Millions", 2_475_260, "$2.47M"}, | ||
{"Thousands", 2_475, "$2.47K"}, | ||
{"Hundreds", 247, "$247"}, | ||
{"Tens", 24, "$24"}, | ||
{"NaN", math.NaN(), "NaN"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Finance(tt.arg); got != tt.want { | ||
t.Errorf("Finance() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My own two cents:
If you need to ignore something that is specific to a project it belongs in the project's
.gitignore
If you need to ignore something that is specific to the developer it belongs in the developer's global
.gitignore
.Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove the .gitignore from the PR