1 |
|
2 |
|
3 | # Function createUnit
|
4 |
|
5 | Create a user-defined unit and register it with the Unit type.
|
6 |
|
7 |
|
8 | ## Syntax
|
9 |
|
10 | ```js
|
11 | math.createUnit({
|
12 | baseUnit1: {
|
13 | aliases: [string, ...]
|
14 | prefixes: object
|
15 | },
|
16 | unit2: {
|
17 | definition: string,
|
18 | aliases: [string, ...]
|
19 | prefixes: object,
|
20 | offset: number
|
21 | },
|
22 | unit3: string // Shortcut
|
23 | })
|
24 | ```
|
25 |
|
26 | ### Parameters
|
27 |
|
28 | Parameter | Type | Description
|
29 | --------- | ---- | -----------
|
30 | `name` | string | The name of the new unit. Must be unique. Example: 'knot'
|
31 | `definition` | string, Unit | Definition of the unit in terms of existing units. For example, '0.514444444 m / s'.
|
32 | `options` | Object | (optional) An object containing any of the following properties:</br>- `prefixes {string}` "none", "short", "long", "binary_short", or "binary_long". The default is "none".</br>- `aliases {Array}` Array of strings. Example: ['knots', 'kt', 'kts']</br>- `offset {Numeric}` An offset to apply when converting from the unit. For example, the offset for celsius is 273.15. Default is 0.
|
33 |
|
34 | ### Returns
|
35 |
|
36 | Type | Description
|
37 | ---- | -----------
|
38 | Unit | The new unit
|
39 |
|
40 |
|
41 | ## Examples
|
42 |
|
43 | ```js
|
44 | math.createUnit('foo')
|
45 | math.createUnit('knot', {definition: '0.514444444 m/s', aliases: ['knots', 'kt', 'kts']})
|
46 | math.createUnit('mph', '1 mile/hour')
|
47 | ```
|
48 |
|
49 |
|
50 | ## See also
|
51 |
|
52 | [unit](unit.md)
|