Home Reference Source
import {Formatter} from 'swarm-numberformat/src/format.js'
public class | source

Formatter

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public

opts: *

Method Summary

Public Methods
public

format(val: number, opts: Object): string

Format a number.

public

formatFlavor(val: number, flavor: string, opts: Object): string

Format a number with a specified flavor.

public

index(val: number, opts: Object): number

public
public

suffix(val: number, opts: Object): string

Public Constructors

public constructor(opts: Object) source

Params:

NameTypeAttributeDescription
opts Object

All formatter configuration.

opts.flavor string
  • optional
  • default: 'full'

'full' or 'short'. Flavors can modify any number of other options here. Full is the default; short has fewer sigfigs and shorter standard-suffixes.

opts.flavors Object
  • optional

Specify your own custom flavors.

opts.backend string
  • optional
  • default: 'native'

'native' or 'decimal.js'.

opts.suffixGroup string
  • optional
opts.suffixFn Function
  • optional
opts.minSuffix number
  • optional
  • default: 1e5
opts.maxSmall number
  • optional
  • default: 0

Special formatting for numbers with a decimal point

opts.sigfigs number
  • optional
  • default: 5
opts.format number
  • optional
  • default: 'standard'

'standard', 'hybrid', 'scientific', 'longScale'.

opts.formats Object
  • optional

Specify your own custom formats.

opts.Decimal Function
  • optional

With the decimal.js backend, use this custom decimal.js constructor, like decimal.js-light or break_infinity.js. By default, we'll try to import decimal.js.

Public Members

public opts: * source

Public Methods

public format(val: number, opts: Object): string source

Format a number.

Params:

NameTypeAttributeDescription
val number
opts Object
  • optional

Override the options provided to the Formatter constructor.

Return:

string

The formatted number.

Example:

new Formatter().format(1e6)
// => "1.0000 million"

public formatFlavor(val: number, flavor: string, opts: Object): string source

Format a number with a specified flavor. It's very common to call the formatter with different flavors, so it has its own shortcut.

Formatter.formatFull() and Formatter.formatShort() are also available.

Params:

NameTypeAttributeDescription
val number
flavor string

'short' or 'full'. See opts.flavor.

opts Object
  • optional

Return:

string

The formatted number.

Example:

new Formatter().format(1e6, 'short')
// => "1.00M"

public index(val: number, opts: Object): number source

Params:

NameTypeAttributeDescription
val number
opts Object
  • optional

Return:

number

which suffix to use for this number in a list of suffixes. You can also think of this as "how many commas are in the number?"

public listFormats(opts: Object): string[] source

Params:

NameTypeAttributeDescription
opts Object
  • optional

Return:

string[]

The complete list of formats available. Use this to build an options UI to allow your players to choose their favorite format.

public suffix(val: number, opts: Object): string source

Params:

NameTypeAttributeDescription
val number
opts Object
  • optional

Return:

string

The suffix that this number would use, with no number shown.

Example:

new Formatter().suffix(1e6)
// => " million"
new Formatter().suffix(1e6, {flavor: "short"})
// => "M"