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(numerator, denominator)
math.fraction({n: numerator, d: denominator})
math.fraction(matrix: Array | Matrix)

Parameters #

Parameter Type Description
args number | string | Fraction | BigNumber | 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

Fork me on GitHub