Skip to main content

    Units

    Package for converting and formatting units and numerical values.

    npm install @dynatrace-sdk/units

    Conversion example

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

    convert(1000,
    // units.<category>.<unit>
    units.length.meter, // FromUnit
    units.length.kilometer, // ToUnit
    ); // 1

    Functions

    abbreviateNumber

    abbreviateNumber(input,scale,options): Object

    Abbreviate large numbers to a shorter format. Example:

    import { abbreviateNumber } from '@dynatrace-sdk/units';
    abbreviateNumber(1500) // {"formattedValue": "2", "postfix": "k"}

    Parameters

    NameTypeDescription
    input*requirednumberNumber to be abbreviated.
    scale*requiredScale

    Scale to use for the abbreviation. Default is the decimal scale:

    • abbreviateNumber(1_000) // {"formattedValue": "1", "postfix": "k"}
    • abbreviateNumber(1_000_000, ExponentialOctalByteLevels) // {"formattedValue": "977", "postfix": "KiB"}
    options*requiredAdjustFractionDigitsOptions

    adjustFractionDigits

    adjustFractionDigits(input,options?): string

    Adjust number of fractional digits.

    Parameters

    NameTypeDescription
    input*requirednumberThe number to be formatted.
    optionsAdjustFractionDigitsOptions

    Returns

    Description
    The formatted number as a string.

    convert

    convert(input,from,to): number

    Converts a given number to the specified unit. Example:

    import { convert, units } from "@dynatrace-sdk/units";
    convert(1500, units.length.meter, units.length.kilometer) // 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

    Description
    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<FromUnit | ToUnit>Formatting options.

    Returns

    Description
    A string containing the formatted number.

    formatCurrency

    formatCurrency(number,currency,options?): string

    Formats a number with the currency indicated

    Parameters

    NameTypeDescription
    number*requirednumberThe value to be formatted
    currency*requiredundefined | "CHF" | "AED" | "AFN" | "ALL" | "AMD" | "ANG" | "AOA" | "ARS" | "AUD" | "AWG" | "AZN" | "BAM" | "BBD" | "BDT" | "BGN" | "BHD" | "BIF" | "BMD" | "BND" | "BOB" | "BRL" | "BSD" | "BTN" | "BWP" | "BYN" | "BZD" | "CAD" | "CDF" | "CLP" | "CNY" | "COP" | "CRC" | "CUC" | "CUP" | "CVE" | "CZK" | "DJF" | "DKK" | "DOP" | "DZD" | "EGP" | "ERN" | "ETB" | "EUR" | "FJD" | "FKP" | "GBP" | "GEL" | "GHS" | "GIP" | "GMD" | "GNF" | "GTQ" | "GYD" | "HKD" | "HNL" | "HRK" | "HTG" | "HUF" | "IDR" | "ILS" | "INR" | "IQD" | "IRR" | "ISK" | "JMD" | "JOD" | "JPY" | "KES" | "KGS" | "KHR" | "KMF" | "KPW" | "KRW" | "KWD" | "KYD" | "KZT" | "LAK" | "LBP" | "LKR" | "LRD" | "LSL" | "LYD" | "MAD" | "MDL" | "MGA" | "MKD" | "MMK" | "MNT" | "MOP" | "MRU" | "MUR" | "MVR" | "MWK" | "MXN" | "MYR" | "MZN" | "NAD" | "NGN" | "NIO" | "NOK" | "NPR" | "NZD" | "OMR" | "PAB" | "PEN" | "PGK" | "PHP" | "PKR" | "PLN" | "PYG" | "QAR" | "RON" | "RSD" | "RUB" | "RWF" | "SAR" | "SBD" | "SCR" | "SDG" | "SEK" | "SGD" | "SHP" | "SLE" | "SLL" | "SOS" | "SRD" | "SSP" | "STN" | "SVC" | "SYP" | "SZL" | "THB" | "TJS" | "TMT" | "TND" | "TOP" | "TRY" | "TTD" | "TWD" | "TZS" | "UAH" | "UGX" | "USD" | "UYU" | "UZS" | "VES" | "VND" | "VUV" | "WST" | "XAF" | "XCD" | "XCG" | "XDR" | "XOF" | "XPF" | "XSU" | "YER" | "ZAR" | "ZMW" | "ZWL" |

    The ISO 4217 currency codes, such as 'USD' or 'EUR', to which the number will be formatted. This list is taken directly from INTL object

    optionsFormatCurrencyOptionsOptions to adapt the formatting (locale and properties accepted by Intl.NumberFormat)

    Returns

    Description
    the formatted number to the specified currency

    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

    Description
    Date string

    formatLong

    formatLong(value,options?): string

    Parameters

    NameType
    value*requiredstring | number | bigint
    optionsFormatLong

    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<FromUnit | ToUnit>Formatting options.

    Returns

    Description
    An Array of parts containing the formatting result object.

    mapGrailUnit

    mapGrailUnit(grailUnit): GrailUnitEquivalency

    Finds the corresponding units value based on the Grail string we are passing

    Parameters

    NameTypeDescription
    grailUnit*requiredstringThe grail string which we want to obtain its corresponding unit equivalent

    Returns

    Description
    Object containing the corresponding equivalent grail value to units

    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
    candidateDEPRECATEDnull | string
    relativeDate*requiredDEPRECATEDnumber

    parseTimeAsTimeValue

    parseTimeAsTimeValue(candidate?,relativeDate?,precision?): TimeValue | null

    You can use the parseTime utility function to convert a string into TimeValue, 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

    NameTypeDescription
    candidatenull | stringThe input string to be parsed into a TimeValue.
    relativeDatenumberAn optional number representing a relative date.
    precision"day" | "minutes" | "seconds" | "milliseconds"

    Specifies the level of precision for the parsed time. Can be one of the following:

    • 'day': Only the date part is considered, ignoring the time.
    • 'minutes': Includes the date and time up to minutes.
    • 'seconds': Includes the date and time up to seconds.
    • 'milliseconds': Includes the date and time up to milliseconds.
    • If empty or undefined, the original precision of the candidate is used.

    Returns

    Description
    A TimeValue object containing the normalized input string, a Date object,
    and the type of input, or null if the input can't be converted.

    variantNames

    variantNames(unit): VariantNames<U>

    Returns the names of all units to which the provided unit can be converted to.

    Parameters

    NameType
    unit*requiredU

    variantUnits

    variantUnits(unit): VariantUnits<U>

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

    Parameters

    NameType
    unit*requiredU

    Variables

    ExponentialDecimalLevels: Object

    Abbreviation levels for decimal metric prefixes from kilo to quecto, 10^3, 10^6, ...

    ExponentialOctalBitLevels: Object

    Abbreviation levels for bit binary prefixes from kilobit to quebibit, 2^10, 2^20, ...

    ExponentialOctalByteLevels: Object

    Abbreviation levels for byte binary prefixes from kibibyte to quebibyte, 2^10, 2^20, ...

    timeframeTranslations: Object

    units: Object

    Grouped collection of all the supported units

    Interfaces

    AdjustFractionDigitsOptions

    Properties

    NameTypeDescription
    localestring | Array<string>The locale according to which the number will be adjusted.
    maximumFractionDigitsnumberMaximum number of decimal digits.
    maximumSignificantDigitsnumberMaximum number of significant digits. Default is 21
    minimumFractionDigitsnumberMinimum number of decimal digits.
    minimumSignificantDigitsnumberMinimum number of significant digits. Default is 1
    useGroupingbooleanWhether to use grouping separators, such as thousands separators or thousand/lakh/crore separators.

    FormatCurrencyOptions

    Properties

    NameTypeDescription
    abbreviateboolean

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

    localestring | Array<string>The locale according to which the number will be formatted.

    FormatDateOptions

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

    Properties

    NameTypeDescription
    localestring | Array<string>The locale according to which the number will be formatted.

    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 depth. 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. If minimumFractionDigits is passed, set to minimumFractionDigits by default. Otherwise set to 0 by default.

    maximumSignificantDigitsnumberMaximum number of significant digits. Default is 21
    minimumFractionDigitsnumber

    The amount of minimumFractionDigits points. See MDN.

    minimumSignificantDigitsnumberMinimum number of significant digits. Default is 1
    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.
    useGroupingboolean

    Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators See MDN

    ConvertibleUnit

    All supported convertible units. For more details see a conversion example.

    import { units } from '@dynatrace-sdk/units';
    units.length.meter // extends ConvertibleUnit

    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 can be formatted. Example:

    import { units } from '@dynatrace-sdk/units';
    units.currency.eur // extends FormattableUnit

    Unit

    A ConvertibleUnit or a FormattableUnit or both.

    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