compactnumber

module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2019 License: MIT

README

Compact Number Formats

A wrapper over golang.org/x/text with support for compact number formats in various languages. For example, in English, 19,000,000 has the compact formats of 19 million (long) or 19M (short), but this varies widely across languages. You can see Unicode's documentation of Compact Number Formats here.

This implementation should be considered best-effort. I hope to eventually add this functionality to the core golang.org/x/text repository (see my proposal here).

Note that the Format truncates the number to the nearest "whole number" on the relevant scale (1,999,999 becomes 1M), and fractional compact numbers (e.g. 2.5B) are currently not supported. However, it would be possible to add and I hope to do so at some point in the future.

Usage

import (
	"fmt"

	"github.com/nkall/compactnumber/compact"
	"golang.org/x/text/language"
)

func main() {
	enLang := language.Make("en-US")
	formatter := compact.NewFormatter(enLang, compact.Short)
	out, err := formatter.Format(17999999)
	if err != nil {
		panic(err)
	}

	fmt.Println(out) // 17M
}

Generating Compact Forms

Compact forms can be regenerated with the latest CLDR data by following these steps:

  1. Download the latest JSON CLDR distribution from https://github.com/unicode-cldr/cldr-numbers-modern
  2. Extract the contents of the main directory to compactnumber/cldr.
  3. Run make generate and check in the updated file forms.gen.go.

Directories

Path Synopsis
cmd
Generated by https://github.com/nkall/compactnumber at 2019-10-19 18:08:25.484779 -0700 PDT m=+0.108772910 Based on https://github.com/unicode-cldr/cldr-numbers-modern version 36 Do not edit this file manually! Instead, follow the "Generating Compact Forms" guide in the README.
Generated by https://github.com/nkall/compactnumber at 2019-10-19 18:08:25.484779 -0700 PDT m=+0.108772910 Based on https://github.com/unicode-cldr/cldr-numbers-modern version 36 Do not edit this file manually! Instead, follow the "Generating Compact Forms" guide in the README.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL