Function fraction #

Create a fraction or convert a value to a fraction.

With one numeric argument, produces the closest rational approximation to the input. With two arguments, the first is the numerator and the second is the denominator, and creates the corresponding fraction. Both numerator and denominator must be integers. With one object argument, looks for the integer numerator as the value of property ‘n’ and the integer denominator as the value of property ‘d’. With a matrix argument, creates a matrix of the same shape with entries converted into fractions.

Syntax #

math.fraction(value)
math.fraction(numerator, denominator)
math.fraction({n: numerator, d: denominator})
math.fraction(matrix: Array | Matrix)

Parameters #

Parameter Type Description
args number | string | Fraction | BigNumber | bigint | Unit | Array | Matrix Arguments specifying the value, or numerator and denominator of the fraction

Returns #

Type Description
Fraction | Array | Matrix Returns a fraction

Throws #

Type | Description —- | ———–

Examples #

math.fraction(6.283)             // returns Fraction 6283/1000
math.fraction(1, 3)              // returns Fraction 1/3
math.fraction('2/3')             // returns Fraction 2/3
math.fraction({n: 2, d: 3})      // returns Fraction 2/3
math.fraction([0.2, 0.25, 1.25]) // returns Array [1/5, 1/4, 5/4]
math.fraction(4, 5.1)            // throws Error: Parameters must be integer

See also #

bignumber, number, string, unit

History #

Version Comment
v2 Created
v3 Added conversion from BigNumber
v11.8 Added conversion from Unit
v13 Added conversion from bigint
v14 Move to bigint-based fraction.js@5; construct from two bigints
Fork me on GitHub