-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
Add template func numFormat #1450
Add template func numFormat #1450
Conversation
The template function is currently called |
Also, it might be useful to allow the caller to choose what separators to use. E.g. 1,000.00 is fine in the US, whereas in NL we prefer 1.000,00. |
@ariejan That's already possible. I used this github gist for the implementation. The highlighted lines give you some examples for formating options. And regarding the name: it would be interesting what @bep, @spf13 or the others think about |
I'm not totally married to having to maintain this code - it is a much needed functionality, but it should be in or near Go's stdlib. In any case, it shouldn't live in tempalte_funcs.go, that is too crowded as is. |
I can understand that it means extra work if a bug occurs. If you stick with this code, let it me know and I revisit the code. Another option would be go-humanize. It seems to be widely used (according to the repo stars) and has a good test coverage. On the one hand would this package give as even more options to format content. On the other hand does this mean to add another dependency. If both options aren't fit I'll either close this issue or leave it open as a work in progress. |
My main concern is support. It's nice to just point the finger at a well documented project, in stead of having to dig into the code. |
The package was just meant as an alternative. If you're focused on the script of the Gist regarding support, then we've just two options: either we dig into the code and maintain it if neccessary or we exclude the Gist as option entirely. |
To conclude: The code looks clean enough, but we're not staffed to maintain/support it. |
I've moved the helper functions, that are used to render the numbers, into their own file. |
return renderFloat(format, conv) | ||
default: | ||
return "", fmt.Errorf("Invalid type to format.") | ||
} |
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.
Couple questions: shouldn't this switch handle all int and float types? And why have renderInteger
when you just work with floats anyway? Just use a single func and do the type casting here.
switch conv := num.(type) {
case int, int8, int16, int32, int64, float32, float64:
return renderNumber(format, conv.(float64))
default:
return "", fmt.Errorf("Invalid type to format.")
}
49b4f8e
to
93e41a1
Compare
numFormat can format floats and integers with custom thousands seperators and decimal seperators. Users can also the define the precision up to 9 digits after the comma. Fixes #1444.
Closing this in favor of #2851 |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
numFormat can format floats and integers with custom thousands seperators and decimal seperators. Users can also the define the precision up to 9 digits after the comma.
Fixes #1444.