Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Add template func numFormat #1450

wants to merge 1 commit into from

Conversation

digitalcraftsman
Copy link
Member

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.

@digitalcraftsman
Copy link
Member Author

The template function is currently called formatnum. Does someone prefer instead a shorthand like numf similar to Errorf and Printf of the fmt package?

@ariejan
Copy link
Contributor

ariejan commented Sep 25, 2015

Numf sounds nice.

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.

@digitalcraftsman
Copy link
Member Author

@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 formatnum vs numf.

@bep
Copy link
Member

bep commented Sep 25, 2015

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.

@digitalcraftsman
Copy link
Member Author

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.

@bep
Copy link
Member

bep commented Sep 25, 2015

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.

@digitalcraftsman
Copy link
Member Author

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.

@bep
Copy link
Member

bep commented Sep 25, 2015

To conclude: The code looks clean enough, but we're not staffed to maintain/support it.

@digitalcraftsman
Copy link
Member Author

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.")
}
Copy link
Contributor

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.")
}

@digitalcraftsman digitalcraftsman changed the title Add template func formatnum Add template func numFormat Apr 9, 2016
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.
@digitalcraftsman
Copy link
Member Author

Closing this in favor of #2851

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Outputting numbers with thousands separators
4 participants