Formatter
Constructor Summary
Public Constructor | ||
public |
constructor(opts: Object) |
Member Summary
Public Members | ||
public |
opts: * |
Method Summary
Public Methods | ||
public |
Format a number. |
|
public |
formatFlavor(val: number, flavor: string, opts: Object): string Format a number with a specified flavor. |
|
public |
|
|
public |
listFormats(opts: Object): string[] |
|
public |
|
Public Constructors
public constructor(opts: Object) source
Params:
Name | Type | Attribute | Description |
opts | Object | All formatter configuration. |
|
opts.flavor | string |
|
'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 |
|
Specify your own custom flavors. |
opts.backend | string |
|
'native' or 'decimal.js'. |
opts.suffixGroup | string |
|
|
opts.suffixFn | Function |
|
|
opts.minSuffix | number |
|
|
opts.maxSmall | number |
|
Special formatting for numbers with a decimal point |
opts.sigfigs | number |
|
|
opts.format | number |
|
'standard', 'hybrid', 'scientific', 'longScale'. |
opts.formats | Object |
|
Specify your own custom formats. |
opts.Decimal | Function |
|
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.
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.
Example:
new Formatter().format(1e6, 'short')
// => "1.00M"
public index(val: number, opts: Object): number source
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:
Name | Type | Attribute | Description |
opts | Object |
|
Return:
string[] | The complete list of formats available. Use this to build an options UI to allow your players to choose their favorite format. |