Fraction

Enables the safe handling of fractional amounts.

GitHub File: fraction.ts

Properties

Public

PropertyTypeModifierDescription

numerator

readonly

The fraction's numerator.

denominator

readonly

The fraction's denominator.

Constructor

Parameters

ParamsTypeDescription

numerator

The numerator of the fraction to be created.

denominator

The denominator of the fraction to be created.

Methods

numberatorBN() - public get

Returns the Big Number equivalent of the numerator.

Note the typo in the method name as the extra b has not been removed to avoid any disruptions.

Returns

TypeDescription

The fraction's numerator returned as a Big Number type.


denominatorBN() - public get

Returns the Big Number equivalent of the denominator.

Returns

TypeDescription

The fraction's denominator returned as a Big Number type.


tryParseFunction() - private static

Validates that the parameter passed is a fraction and returns the fraction.

Parameters

ParamsTypeDescription

fractionish

BigintIsh | Fraction

The fraction to be parsed/validated.

Returns

TypeDescription

Fraction

The parsed Fraction


quotient() - public get

Gets the result of dividing the fraction rounded down to the nearest integer.

Returns

TypeDescription

The quotient when the numerator is divided by the denominator.


quotientBN() - public get

Gets the result of dividing the fraction rounded down to the nearest integer as a Big Number type.

Returns

TypeDescription

The quotient when the numerator is divided by the denominator.


remainder() - public get

Gets the remainder of dividing the fraction rounded down to the nearest integer.

Returns

TypeDescription

Fraction

The remainder Fraction when the numerator is divided by the denominator.


invert() - public

Returns the inverted fraction of the Fraction instance.

Returns

TypeDescription

Fraction

The fraction instance inverted.


add() - public

Adds the provided fraction to the Fraction instance and returns the result as a new Fraction.

Parameters

ParamsTypeDescription

other

Fraction | BigintIsh

The fraction to be added to the Fraction instance.

Returns

TypeDescription

Fraction

The fraction representing the sum of the two fractions.


subtract() - public

Subtracts/minuses the provided fraction to the Fraction instance and returns the result as a new Fraction.

Parameters

ParamsTypeDescription

other

Fraction | BigintIsh

The fraction to be subtracted from the Fraction instance.

Returns

TypeDescription

Fraction

The final fraction after subtracting the given fraction from the Fraction instance.


lessThan() - public

Checks if the Fraction instance is less than the provided fraction.

Parameters

ParamsTypeDescription

other

Fraction | BigintIsh

The fraction to compare against the Fraction instance.

Returns

TypeDescription

boolean

true = Fraction instance is less than provided fraction. false = Fraction instance is greater than provided fraction.


equalTo() - public

Checks if the Fraction instance is equal to the provided fraction.

Parameters

ParamsTypeDescription

other

Fraction | BigintIsh

The fraction to compare against the Fraction instance.

Returns

TypeDescription

boolean

true = Fraction instance is equal to the provided fraction. false = Fraction instance is not equal to the provided fraction.


greaterThan() - public

Checks if the Fraction instance is greater than the provided fraction.

Parameters

ParamsTypeDescription

other

Fraction | BigintIsh

The fraction to compare against the Fraction instance.

Returns

TypeDescription

boolean

true = Fraction instance is greater than provided fraction. false = Fraction instance is less than provided fraction.


multiply() - public

Multiplies the Fraction instance with the provided fraction and returns the result as a new Fraction.

Parameters

ParamsTypeDescription

other

Fraction | BigintIsh

The fraction to be multiplied with the Fraction instance.

Returns

TypeDescription

Fraction

The final fraction after multiplying the given fraction with the Fraction instance.


divide() - public

Divides the Fraction instance with the provided fraction and returns the result as a new Fraction.

Parameters

ParamsTypeDescription

other

Fraction | BigintIsh

The fraction to be divided from the Fraction instance.

Returns

TypeDescription

Fraction

The final fraction after dividing the Fraction instance by the provided fraction.


toSignificant() - public

Rounds the Fraction to the significant digits specified.

Parameters

ParamsTypeDescription

significantDigits

number

The number of significant digits to round to.

format

object

Configure output format per toformat specifications.

rounding

Specifies the rounding logic used. Default is ROUND_HALF_UP which rounds to the nearest neighbour and rounds up if equidistant.

Returns

TypeDescription

string

The final quotient value rounded to the specified significant digits.


toFixed() - public

Rounds the Fraction to the fixed number of decimals specified.

Parameters

ParamsTypeDescription

decimalPlaces

number

The number of decimal places to round to.

format

object

Configure output format per toformat specifications.

rounding

Specifies the rounding logic used. Default is ROUND_HALF_UP which rounds to the nearest neighbour and rounds up if equidistant.

Returns

TypeDescription

string

The final quotient value rounded to the decimal places specified.


asFraction() - public get

Helper method for converting any super class back to a fraction.

Returns

TypeDescription

Fraction

Fraction representation of the super class.

Last updated