Skip to main content

Units

Tools for converting and formatting the units and numerical values.

npm install @dynatrace-sdk/units

Conversion example

Code example
import { units, convert } from "@dynatrace-sdk/units";

// units.{unitGroup}.{unit}
convert(
1000,
/* FromUnit */ units.length.meter,
/* ToUnit */ units.length.kilometer
);

Interfaces

AbbreviateOptions

Properties

NameTypeDescription
localestring | Array<string>The locale according to which the number will abbreviate.
maximumFractionDigitsnumberMaximum number of decimal places.
minimumFractionDigitsnumberMinimum number of decimal places.

AdjustFractionDigitsOptions

Properties

NameTypeDescription
localestring | Array<string>The locale according to which the number will be adjusted.
maximumFractionDigitsnumberMaximum number of decimal places.
minimumFractionDigitsnumberMinimum number of decimal places.

FormatDateOptions

Interface that extends Intl.DateTimeFormatOptions to pass an optional property to overwrite the default locale. See MDN.

Properties

NameTypeDescription
calendarstring
dateStyle"full" | "long" | "medium" | "short"
day"numeric" | "2-digit"
dayPeriod"long" | "short" | "narrow"
era"long" | "short" | "narrow"
formatMatcher"best fit" | "basic"
fractionalSecondDigits1 | 2 | 3
hour"numeric" | "2-digit"
hour12boolean
hourCycle"h11" | "h12" | "h23" | "h24"
localestring | Array<string>The locale according to which the number will be formatted.
localeMatcher"best fit" | "lookup"
minute"numeric" | "2-digit"
month"long" | "short" | "narrow" | "numeric" | "2-digit"
numberingSystemstring
second"numeric" | "2-digit"
timeStyle"full" | "long" | "medium" | "short"
timeZonestring
timeZoneName"long" | "short" | "shortOffset" | "longOffset" | "shortGeneric" | "longGeneric"
weekday"long" | "short" | "narrow"
year"numeric" | "2-digit"

FormatOptions

Properties

NameTypeDescription
abbreviateboolean

It will shorten the number to a shorter format. (e.g. input: 1500, output: 1.5k).

cascadenumber

Will search for the biggest unit within its group and cascade it down the the specified dept. The last number will get rounded to maximumFractionDigits. (e.g. format(1500, { input: units.length.meter, cascade: 2}), output: "1 km 500 m")).

inputFromUnit

If input unit is defined, it will convert it to the best fitting unit. (e.g. format(1500, { input: units.length.meter }), output: "1.5 km")).

localestring | Array<string>

The locale that will be used to format the number. By default it will use the platform locale specified by the user.

maximumFractionDigitsnumber

The amount of maximumFractionDigits points. See MDN.

minimumFractionDigitsnumber

The amount of minimumFractionDigits points. See MDN.

outputOutputUnit<UndefinedCoalescing<FromUnit | > | ToUnit>

If not specified, the conversion is disabled (e.g. format(1500), output: 1.5K).

suffixstringA custom suffix that overwrites the unit symbol.

Variables

ExponentialDecimalLevels: Array<Object>

Levels of abbreviations for decimal exponents from kilo to nonillion, 10^3, 10^6, ...

ExponentialOctalBitLevels: Array<Object>

Levels of abbreviation for octal bit exponents from kilobit to quebibit, 2^10, 2^20, 2^30...

ExponentialOctalByteLevels: Array<Object>

Levels of abbreviation for octal byte exponents from kibibyte to quebibyte, 2^10, 2^20, 2^30...

units: Object

Grouped collection of all the supported units

Functions

abbreviateNumber

abbreviateNumber(input,scale,options?): Object

Abbreviates large numbers to a shorter format.

  • input: 1500, output: 1.5K

Parameters

NameTypeDescription
input*requirednumberNumber that will get abbreviated.
scale*requiredArray<Object>

Scale for the abbreviation. Default is decimal scale:

  • input: 1_000, output: 1K
  • input: 1_000_0000, output: 1M
optionsAbbreviateOptions

adjustFractionDigits

adjustFractionDigits(input,options?): string

Adjust number of decimals.

Parameters

NameTypeDescription
input*requirednumberThe number to be formatted.
optionsAdjustFractionDigitsOptions

Returns

A formatted number as a string.

convert

convert(input,from,to): number

Converts a given number to the specified unit. (e.g. convert(1500, units.length.meter, unit.length.kilometer), output: 1.5).

Parameters

NameTypeDescription
input*requirednumberThe number that will get converted.
from*requiredFromUnitThe unit of the input (e.g. units.length.meter).
to*requiredConvertibleTarget<FromUnit | ToUnit>The unit of the output (e.g. units.length.kilometer).

Returns

The result of the conversion (e.g. 1.5).

format

format(number,options?): string

Converts and formats the provided number.

Parameters

NameTypeDescription
number*requirednumberThe number that will get formatted.
optionsFormatOptions<UndefinedCoalescing<FromUnit | > | ToUnit>Formatting options.

Returns

A string containing the formatted number.

formatDate

formatDate(input,options?): string

Formats a date according to locale.

Parameters

NameTypeDescription
input*requirednumber | DateNumber of milliseconds since UNIX epoch or a Javascript Date.
optionsFormatDateOptionsAn object that contains one or more properties that specify comparison options.

Returns

Date string

formatUnit

formatUnit(unit): string

Formats a unit to a string.

Parameters

NameType
unit*requiredFormattableUnit

getFormatting

getFormatting(number,options?): Formatting

Converts and formats the provided number, but returns the formatting as parts.

Parameters

NameTypeDescription
number*requirednumberThe number that will get formatted.
optionsFormatOptions<UndefinedCoalescing<FromUnit | > | ToUnit>Formatting options.

Returns

An Array of parts containing the formatting result object.

parseTime

parseTime(candidate?,relativeDate): null | TimeDetails

You can use the parseTime utility function to convert a string into TimeDetails, giving you a normalized version of the input string (trimmed string), a Date object created from the given input, and the type of input. The type can be either an expression or iso8601 string. If it is not possible to convert the input, the function will return null.

Parameters

NameType
candidatenull | string
relativeDate*requirednumber

variantNames

variantNames(unit): VariantNames<U>

Provides all the unit names to which the provided unit can be converted to.

Parameters

NameType
unit*requiredU

variantUnits

variantUnits(unit): VariantUnits<U>

Provides all the units to which the provided unit can be converted to.

Parameters

NameType
unit*requiredU

ConvertibleUnit

All supported convertible units. For more details see a conversion example.
import { units } from "@dynatrace-sdk/units";

units.length.meter; // is a convertible unit

FromUnit

A type for every convertible unit. For more details see a conversion example.

ToUnit

All units to which the `FromUnit` can be converted. For more details see a conversion example.

FormattableUnit

A unit that is supported by our formatting functionality.

ConvertibleTarget

TypeScript helper that returns all the possible convertible targets, where the first generic is FromUnit, and second is ToUnit.

Still have questions?
Find answers in the Dynatrace Community