Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jackc/numfmt
Number formatting in Go
https://github.com/jackc/numfmt
Last synced: 24 days ago
JSON representation
Number formatting in Go
- Host: GitHub
- URL: https://github.com/jackc/numfmt
- Owner: jackc
- License: mit
- Created: 2021-02-06T21:40:03.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-02-09T20:11:00.000Z (over 3 years ago)
- Last Synced: 2024-06-20T15:54:35.824Z (5 months ago)
- Language: Go
- Size: 24.4 KB
- Stars: 9
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Go Reference](https://pkg.go.dev/badge/github.com/jackc/numfmt.svg)](https://pkg.go.dev/github.com/jackc/numfmt)
![CI](https://github.com/jackc/numfmt/workflows/CI/badge.svg)# numfmt
`numfmt` is a number formatting package for Go.
## Features
* Rounding to N decimal places
* Always display minimum of N decimal places
* Configurable thousands separators
* Scaling for percentage formatting
* Format negative values differently for correct currency output like `-$12.34` or `(12.34)`
* Easy to use with `text/template` and `html/template`## Examples
Use directly from Go:
```go
f := &numfmt.Formatter{
NegativeTemplate: "(n)",
MinDecimalPlaces: 2,
}
f.Format("-1234") // => "(1,234.00)"
```Or in use in `text/template`:
```
{{numfmt "1234.5"}} => "1,234.5"
{{numfmt "GroupSeparator" " " "DecimalSeparator" "," "1234.5"}} => "1 234,5"
```See the [documentation](https://pkg.go.dev/github.com/jackc/numfmt) for more examples.