-
Notifications
You must be signed in to change notification settings - Fork 233
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
money notation #95
Open
lfaoro
wants to merge
4
commits into
dustin:master
Choose a base branch
from
lfaoro:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
money notation #95
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package humanize | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
var ( | ||
FinanceSign = "$" | ||
) | ||
|
||
func Finance(f float64) string { | ||
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("%s%sQ", FinanceSign, s) | ||
|
||
case n >= 1_000_000_000_000: | ||
s := fmt.Sprintf("%.f", f) | ||
s = insertAt(1, '.', []rune(s[:4])) | ||
return fmt.Sprintf("%s%sT", FinanceSign, s) | ||
|
||
case n >= 1_000_000_000: | ||
s := fmt.Sprintf("%.f", f) | ||
s = insertAt(1, '.', []rune(s[:4])) | ||
return fmt.Sprintf("%s%sB", FinanceSign, s) | ||
|
||
case n >= 1_000_000: | ||
s := fmt.Sprintf("%.f", f) | ||
s = insertAt(1, '.', []rune(s[:4])) | ||
return fmt.Sprintf("%s%sM", FinanceSign, s) | ||
|
||
case n >= 1_000: | ||
s := fmt.Sprintf("%.f", f) | ||
s = insertAt(1, '.', []rune(s[:4])) | ||
return fmt.Sprintf("%s%sK", FinanceSign, s) | ||
|
||
case n < 1_000: | ||
return fmt.Sprintf("%s%.f", FinanceSign, 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 wonder if it would make sense to take some optional
Options
which could set things like which $ sign to use and if it should be prepended or appended (or omitted entirely) and could control the # of decimal points.So you could do things like:
1.83M USD
€2.34T
48.9945
etc. Could also look at the various
locales
used with https://www.php.net/manual/en/function.money-format.phpThere 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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
ping @harrisonhjones