compactnumber

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 4, 2020 License: MIT Imports: 8 Imported by: 1

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

func main() {
	formatter := compactnumber.NewFormatter("en-US", compactnumber.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.

Documentation

Overview

Package compactnumber allows for localized CLDR compact number formatting.

Code generated by https://github.com/nkall/compactnumber at 2019-10-20 13:43:16.072781 -0700 PDT m=+0.107571706. DO NOT EDIT. 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.

Index

Constants

View Source
const (
	// No compaction, e.g. 17,000,000.
	None = CompactType("None")
	// Long-form compaction, e.g. 17 million.
	Long = CompactType("Long")
	// Short-form compaction, e.g. 17M.
	Short = CompactType("Short")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CompactType

type CompactType string

CompactType is an enum used to specify compaction settings.

type Formatter

type Formatter struct {
	// contains filtered or unexported fields
}

Formatter is a struct containing a method to format an integer based on the specified language and compaction type.

func NewFormatter

func NewFormatter(lang string, compactType CompactType) Formatter

NewFormatter creates a new formatter based on the specified language and compaction type.

func (*Formatter) Format

func (f *Formatter) Format(n int, numOptions ...number.Option) (string, error)

Format takes in an integer and options and formats it according to the formatter's locale and compaction settings. Note: this method truncates numbers and does not support fractions (e.g. 11.5M).

Documented in CLDR spec: http://www.unicode.org/reports/tr35/tr35-numbers.html#Compact_Number_Formats

type FormatterAPI

type FormatterAPI interface {
	Format(n int, numOptions ...number.Option) (string, error)
}

FormatterAPI is an interface implemented by Formatter that can be used for mocking purposes

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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