Skip to content

NumberFormatter

Type class

NumberFormatter class. Formats numbers according to specified formats.

Sources

NumberFormatter can be used (imported) via one of the following packages.

/**
* Import from: "core.ts"
* Use like: am4core.NumberFormatter
*/
import * as am4core from "@amcharts/amcharts4/core";

Inheritance

NumberFormatter extends BaseObject.

NumberFormatter is not extended by any other symbol.

Properties

bigNumberPrefixes

Type INumberSuffix[]

Prefixes for big numbers.

It's an array of objects of number/prefix pairs.

[
  { "number": 1e+3, "suffix": "K" },
  { "number": 1e+6, "suffix": "M" },
  { "number": 1e+9, "suffix": "G" },
  { "number": 1e+12, "suffix": "T" },
  { "number": 1e+15, "suffix": "P" },
  { "number": 1e+18, "suffix": "E" },
  { "number": 1e+21, "suffix": "Z" },
  { "number": 1e+24, "suffix": "Y" }
]

If the number is bigger than the number ir will be converted to the appropriate bigger number with prefix.

E.g. as per above 1500 will be converted to 1.5K.

Please note that for this transformation to be enabled, you need to enable it specific modifier in your format setting.

The modifier for big/small number modification is "a":

{myfield.formatNumber("#,###.00a")}

More info: Formatting Numbers Tutorial on number formatting

bytePrefixes

Type INumberSuffix[]

Basically the same as bigNumberPrefixes, except base for calculation is not thousand but byte (1024).

The modifier is "b".

{myfield.formatNumber("#,###.00b")}

The above 2048 will change to 2K.

More info: Formatting Numbers Tutorial on number formatting

forceLTR

Type boolean

intlLocales

Type string

Locales if you are using date formats in Intl.NumberFormatOptions syntax.

More info: (@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat) about using Intl for number formatting

language

Type Language

A reference to Language instance.

Formatter will use language to translate various items, like number suffixes, etc.

negativeBase

Type numberDefault 0

Negative base for negative numbers.

More info: Formatting Numbers Tutorial on number formatting

numberFormat

Type string | NumberFormatOptionsDefault "#,###.#####"

Number format.

More info: Formatting Numbers Tutorial on number formatting

smallNumberPrefixes

Type INumberSuffix[]

Prefixes for big numbers.

It's an array of objects of number/prefix pairs.

[
  { "number": 1e-24, "suffix": "y" },
  { "number": 1e-21, "suffix": "z" },
  { "number": 1e-18, "suffix": "a" },
  { "number": 1e-15, "suffix": "f" },
  { "number": 1e-12, "suffix": "p" },
  { "number": 1e-9, "suffix": "n" },
  { "number": 1e-6, "suffix": "μ" },
  { "number": 1e-3, "suffix": "m" }
]

If the number is smaller than the number ir will be converted to the appropriate smaller number with prefix.

E.g. as per above 0.0015 will be converted to 1.5m.

Please note that for this transformation to be enabled, you need to enable it specific modifier in your format setting.

The modifier for big/small number modification is "a":

{myfield.formatNumber("#,###.00a")}

IMPORTANT: The order of the suffixes is important. The list must start from the smallest number and work towards bigger ones.

More info: Formatting Numbers Tutorial on number formatting

smallNumberThreshold

Type numberSince 4.6.8

Any number smaller than this will be considered "small" number, which will trigger special formatting if "a" format modifier is used.

sprite

Type $type.Optional <Sprite>

Holds reference to parent Sprite object.

4 inherited items
clonedFrom

Type $type.Optional <this>Inherited from BaseObject

Reference to the original object this object was cloned from. We need to keep this so we can disassociate it from source object when this object is disposed.

config

Type objectInherited from BaseObject

Use this property to set JSON-based config. When set, triggers processing routine, which will go through all properties, and try to apply values, create instances, etc.

Use this with caution, as it is a time-consuming process. It's used for initialchart setup only, not routine operations.

id

Type $type.Optional <string>Inherited from BaseObject

Sets the user-defined id of the element.

uid

Type stringInherited from BaseObject

Returns object's internal unique ID.

Methods

constructor()

Returns NumberFormatter

Constructor

dispose()

Returns void

format(value: number | string,
format?: string | NumberFormatOptions,
precision?: number
)

Returns string

Formats the number according to specific format.

4 inherited items
clone(cloneId?: string)

Returns thisInherited from BaseObject

Makes a copy of this object and returns the clone. Try to avoid cloning complex objects like chart, create new instances if you need them.

copyFrom(object: this)

Returns voidInherited from BaseObject

Copies all properties and related data from different element.

getCurrentThemes()

Returns ITheme[]Inherited from BaseObject

Returns a list of themes that should be applied to this element. It could either be a list of themes set explicitly on this element, or system-wide.

isDisposed()

Returns booleanInherited from BaseObject

Returns if this object has been already been disposed.

Events

NumberFormatter does not have any events.