Function unit #

Create a unit. Depending on the passed arguments, the function will create and return a new math.Unit object. When a matrix is provided, all elements will be converted to units.

Syntax #

math.unit(unit : string)
math.unit(value : number, valuelessUnit : Unit)
math.unit(value : number, valuelessUnit : string)

Parameters #

Parameter Type Description
args * | Array | Matrix A number and unit.

Returns #

Type Description
Unit | Array | Matrix The created unit

Throws #

Type | Description —- | ———–

Examples #

const kph = math.unit('km/h')   // returns Unit km/h (valueless)
const v = math.unit(25, kph)    // returns Unit 25 km/h
const a = math.unit(5, 'cm')    // returns Unit 50 mm
const b = math.unit('23 kg')    // returns Unit 23 kg
a.to('m')                       // returns Unit 0.05 m

See also #

bignumber, boolean, complex, index, matrix, number, string, createUnit

History #

Version Comment
v0.5 Created
v0.16 Support conversion from BigNumber
v2.5 Support BigNumber and Fraction values in units
v2.6 Support Complex values in units
v11.1 Allow the type of unit to be specifed by a unit (not just string)
Fork me on GitHub