docs/reference/classes/unit.md
<a name="Unit"></a>
<a name="new_Unit_new"></a>
A unit can be constructed in the following ways:
const a = new Unit(value, name)
const b = new Unit(null, name)
const c = Unit.parse(str)
Example usage:
const a = new Unit(5, 'cm') // 50 mm
const b = Unit.parse('23 kg') // 23 kg
const c = math.in(a, new Unit(null, 'm') // 0.05 m
const d = new Unit(9.81, "m/s^2") // 9.81 m/s^2
| Param | Type | Description |
|---|---|---|
| [value] | <code>number</code> | <code>BigNumber</code> | <code>Fraction</code> | <code>Complex</code> | <code>boolean</code> | A value like 5.2 |
| [name] | <code>string</code> | A unit name like "cm" or "inch", or a derived unit of the form: "u1[^ex1] [u2[^ex2] ...] [/ u3[^ex3] [u4[^ex4]]]", such as "kg m^2/s^2", where each unit appearing after the forward slash is taken to be in the denominator. "kg m^2 s^-2" is a synonym and is also acceptable. Any of the units can include a prefix. |
<a name="Unit+valueOf"></a>
Returns the string representation of the unit.
Kind: instance property of <code>Unit</code>
<a name="Unit+clone"></a>
create a copy of this unit
Kind: instance method of <code>Unit</code>
Returns: <code>Unit</code> - Returns a cloned version of the unit
<a name="Unit+_isDerived"></a>
Return whether the unit is derived (such as m/s, or cm^2, but not N)
Kind: instance method of <code>Unit</code>
Returns: <code>boolean</code> - True if the unit is derived
<a name="Unit+hasBase"></a>
check if this unit has given base unit If this unit is a derived unit, this will ALWAYS return false, since by definition base units are not derived.
Kind: instance method of <code>Unit</code>
| Param | Type |
|---|---|
| base | <code>BASE_UNITS</code> | <code>STRING</code> | <code>undefined</code> |
<a name="Unit+equalBase"></a>
Check if this unit has a base or bases equal to another base or bases For derived units, the exponent on each base also must match
Kind: instance method of <code>Unit</code>
Returns: <code>boolean</code> - true if equal base
| Param | Type |
|---|---|
| other | <code>Unit</code> |
<a name="Unit+equals"></a>
Check if this unit equals another unit
Kind: instance method of <code>Unit</code>
Returns: <code>boolean</code> - true if both units are equal
| Param | Type |
|---|---|
| other | <code>Unit</code> |
<a name="Unit+multiply"></a>
Multiply this unit with another one
Kind: instance method of <code>Unit</code>
Returns: <code>Unit</code> - product of this unit and the other unit
| Param | Type |
|---|---|
| other | <code>Unit</code> |
<a name="Unit+divide"></a>
Divide this unit by another one
Kind: instance method of <code>Unit</code>
Returns: <code>Unit</code> - result of dividing this unit by the other unit
| Param | Type |
|---|---|
| other | <code>Unit</code> |
<a name="Unit+pow"></a>
Calculate the power of a unit
Kind: instance method of <code>Unit</code>
Returns: <code>Unit</code> - The result: this^p
| Param | Type |
|---|---|
| p | <code>number</code> | <code>Fraction</code> | <code>BigNumber</code> |
<a name="Unit+abs"></a>
Calculate the absolute value of a unit
Kind: instance method of <code>Unit</code>
Returns: <code>Unit</code> - The result: |x|, absolute value of x
| Param | Type |
|---|---|
| x | <code>number</code> | <code>Fraction</code> | <code>BigNumber</code> |
<a name="Unit+to"></a>
Convert the unit to a specific unit name.
Kind: instance method of <code>Unit</code>
Returns: <code>Unit</code> - Returns a clone of the unit with a fixed prefix and unit.
| Param | Type | Description |
|---|---|---|
| valuelessUnit | <code>string</code> | <code>Unit</code> | A unit without value. Can have prefix, like "cm" |
<a name="Unit+toNumber"></a>
Return the value of the unit when represented with given valueless unit
Kind: instance method of <code>Unit</code>
Returns: <code>number</code> - Returns the unit value as number.
| Param | Type | Description |
|---|---|---|
| valuelessUnit | <code>string</code> | <code>Unit</code> | For example 'cm' or 'inch' |
<a name="Unit+toNumeric"></a>
Return the value of the unit in the original numeric type
Kind: instance method of <code>Unit</code>
Returns: <code>number</code> | <code>BigNumber</code> | <code>Fraction</code> - Returns the unit value
| Param | Type | Description |
|---|---|---|
| valuelessUnit | <code>string</code> | <code>Unit</code> | For example 'cm' or 'inch' |
<a name="Unit+toString"></a>
Get a string representation of the unit.
Kind: instance method of <code>Unit</code>
<a name="Unit+toJSON"></a>
Get a JSON representation of the unit
Kind: instance method of <code>Unit</code>
Returns: <code>Object</code> - Returns a JSON object structured as:
{"mathjs": "Unit", "value": 2, "unit": "cm", "fixPrefix": false}
<a name="Unit+formatUnits"></a>
Get a string representation of the units of this Unit, without the value.
Kind: instance method of <code>Unit</code>
<a name="Unit+format"></a>
Get a string representation of the Unit, with optional formatting options.
Kind: instance method of <code>Unit</code>
| Param | Type | Description |
|---|---|---|
| [options] | <code>Object</code> | <code>number</code> | <code>function</code> | Formatting options. See lib/utils/number:format for a description of the available options. |
<a name="Unit+toBest"></a>
Converts a unit to the most appropriate display unit with optional unitList and options.
Kind: instance method of <code>Unit</code> Returns: <code>Unit</code>
| Param | Type | Description |
|---|---|---|
| unitList | <code>Array of strings</code> | | Units strings array (optional). |
| options | <code>Object</code> | | Only "offset" available right now - better prefix calculation |
<a name="Unit.parse"></a>
Parse a string into a unit. The value of the unit is parsed as number,
BigNumber, or Fraction depending on the math.js config setting number.
Throws an exception if the provided string does not contain a valid unit or cannot be parsed.
Kind: static method of <code>Unit</code>
Returns: <code>Unit</code> - unit
| Param | Type | Description |
|---|---|---|
| str | <code>string</code> | A string like "5.2 inch", "4e2 cm/s^2" |
<a name="Unit.isValuelessUnit"></a>
Test if the given expression is a unit. The unit can have a prefix but cannot have a value.
Kind: static method of <code>Unit</code>
Returns: <code>boolean</code> - true if the given string is a unit
| Param | Type | Description |
|---|---|---|
| name | <code>string</code> | A string to be tested whether it is a value less unit. The unit can have prefix, like "cm" |
<a name="Unit.fromJSON"></a>
Instantiate a Unit from a JSON object
Kind: static method of <code>Unit</code>
| Param | Type | Description |
|---|---|---|
| json | <code>Object</code> | A JSON object structured as: {"mathjs": "Unit", "value": 2, "unit": "cm", "fixPrefix": false} |