mathjs/docs/reference/functions/createUnit.md
2022-07-23 12:23:14 +02:00

2.0 KiB

layout
default

Function createUnit #

Create a user-defined unit and register it with the Unit type.

Syntax #

math.createUnit({
  baseUnit1: {
    aliases: [string, ...]
    prefixes: object
  },
  unit2: {
    definition: string,
    aliases: [string, ...]
    prefixes: object,
    offset: number
  },
  unit3: string    // Shortcut
})

Parameters #

Parameter Type Description
name string The name of the new unit. Must be unique. Example: 'knot'
definition string, UnitDefinition, Unit Definition of the unit in terms of existing units. For example, '0.514444444 m / s'.
options Object (optional) An object containing any of the following properties:
- prefixes {string} "none", "short", "long", "binary_short", or "binary_long". The default is "none".
- aliases {Array} Array of strings. Example: ['knots', 'kt', 'kts']
- offset {Numeric} An offset to apply when converting from the unit. For example, the offset for celsius is 273.15. Default is 0.

Returns #

Type Description
Unit The new unit

Throws #

Type Description

Examples #

math.createUnit('foo')
math.createUnit('knot', {definition: '0.514444444 m/s', aliases: ['knots', 'kt', 'kts']})
math.createUnit('mph', '1 mile/hour')
math.createUnit('km', math.unit(1000, 'm'))

See also #

unit