KyberSwap Docs
Search
K

Fraction

Enables the safe handling of fractional amounts.
GitHub File: fraction.ts

Properties

Public

Property
Type
Modifier
Description
numerator
JSBI
readonly
The fraction's numerator.
denominator
JSBI
readonly
The fraction's denominator.

Constructor

Parameters

Params
Type
Description
numerator
BigintIsh
The numerator of the fraction to be created.
denominator
BigintIsh
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

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

denominatorBN() - public get

Returns the Big Number equivalent of the denominator.

Returns

Type
Description
BN
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

Params
Type
Description
fractionish
BigintIsh | Fraction
The fraction to be parsed/validated.

Returns

Type
Description
Fraction
The parsed Fraction

quotient() - public get

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

Returns

Type
Description
JSBI
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

Type
Description
BN
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

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

invert() - public

Returns the inverted fraction of the Fraction instance.

Returns

Type
Description
Fraction
The fraction instance inverted.

add() - public

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

Parameters

Params
Type
Description
other
Fraction | BigintIsh
The fraction to be added to the Fraction instance.

Returns

Type
Description
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

Params
Type
Description
other
Fraction | BigintIsh
The fraction to be subtracted from the Fraction instance.

Returns

Type
Description
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

Params
Type
Description
other
Fraction | BigintIsh
The fraction to compare against the Fraction instance.

Returns

Type
Description
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

Params
Type
Description
other
Fraction | BigintIsh
The fraction to compare against the Fraction instance.

Returns

Type
Description
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

Params
Type
Description
other
Fraction | BigintIsh
The fraction to compare against the Fraction instance.

Returns

Type
Description
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

Params
Type
Description
other
Fraction | BigintIsh
The fraction to be multiplied with the Fraction instance.

Returns

Type
Description
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

Params
Type
Description
other
Fraction | BigintIsh
The fraction to be divided from the Fraction instance.

Returns

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

toSignificant() - public

Rounds the Fraction to the significant digits specified.

Parameters

Params
Type
Description
significantDigits
number
The number of significant digits to round to.
format
object
Configure output format per toformat specifications.
rounding
Rounding
Specifies the rounding logic used. Default is ROUND_HALF_UP which rounds to the nearest neighbour and rounds up if equidistant.

Returns

Type
Description
string
The final quotient value rounded to the specified significant digits.

toFixed() - public

Rounds the Fraction to the fixed number of decimals specified.

Parameters

Params
Type
Description
decimalPlaces
number
The number of decimal places to round to.
format
object
Configure output format per toformat specifications.
rounding
Rounding
Specifies the rounding logic used. Default is ROUND_HALF_UP which rounds to the nearest neighbour and rounds up if equidistant.

Returns

Type
Description
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

Type
Description
Fraction
Fraction representation of the super class.
Last modified 5mo ago