Fraction
Enables the safe handling of fractional amounts.
GitHub File: fraction.ts
Properties
Public
Property | Type | Modifier | Description |
---|---|---|---|
numerator | readonly | The fraction's numerator. | |
denominator | readonly | The fraction's denominator. |
Constructor
Parameters
Params | Type | Description |
---|---|---|
numerator | The numerator of the fraction to be created. | |
denominator | The denominator of the fraction to be created. |
Methods
numberatorBN() - public
get
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 |
---|---|
The fraction's numerator returned as a Big Number type. |
denominatorBN() - public
get
public
get
Returns the Big Number equivalent of the denominator.
Returns
Type | Description |
---|---|
The fraction's denominator returned as a Big Number type. |
tryParseFunction() - private
static
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
public
get
Gets the result of dividing the fraction rounded down to the nearest integer.
Returns
Type | Description |
---|---|
The quotient when the numerator is divided by the denominator. |
quotientBN() - public
get
public
get
Gets the result of dividing the fraction rounded down to the nearest integer as a Big Number type.
Returns
Type | Description |
---|---|
The quotient when the numerator is divided by the denominator. |
remainder() - public
get
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
public
Returns the inverted fraction of the Fraction instance.
Returns
Type | Description |
---|---|
Fraction | The fraction instance inverted. |
add() - public
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
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
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 |
|
equalTo() - public
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 |
|
greaterThan() - public
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 |
|
multiply() - public
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
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
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 | Specifies the rounding logic used. Default is |
Returns
Type | Description |
---|---|
string | The final quotient value rounded to the specified significant digits. |
toFixed() - public
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 | Specifies the rounding logic used. Default is |
Returns
Type | Description |
---|---|
string | The final quotient value rounded to the decimal places specified. |
asFraction() - public
get
public
get
Helper method for converting any super class back to a fraction.
Returns
Type | Description |
---|---|
Fraction | Fraction representation of the super class. |
Last updated