The numerator of the fraction.
Optional
denominator: FractionIshThe denominator of the fraction. (default: 1n)
const a = new Fraction(1.23);
const b = new Fraction('1.23');
const c = new Fraction(123, 100);
const d = new Fraction(123n, 100n);
const e = new Fraction(123n, 100n);
const f = new Fraction(1230n, 1000n);
const g = new Fraction(a);
a.eq(b); // true
a.eq(c); // true
a.eq(d); // true
a.eq(e); // true
a.eq(f); // true
a.eq(g); // true
new Fraction('abc'); // throws
new Fraction('invalid NumberIsh'); // throws
Static
Readonly
ZEROStatic
Readonly
ONEReadonly
numeratorThe numerator of the fraction.
Readonly
denominatorThe denominator of the fraction.
Static
Private
_configStatic
configPrivate
configGets the quotient of the fraction (the integer part of the division).
The quotient of the fraction.
new Fraction('123.789').quotient; // 123n
new Fraction('0.789').quotient; // 0n
Gets the remainder of the fraction as a new Fraction instance.
A Fraction instance representing the remainder of the division.
const a = new Fraction(3, 2).remainder;
const b = new Fraction(1, 2);
a.eq(b); // true
const c = new Fraction('123.789').remainder;
const d = new Fraction(123789 % 1000, 1000);
c.eq(d); // true
Static
setSets the configuration by merging with the provided partial configuration.
The partial configuration to merge.
The updated configuration.
Static
Private
parseCreates a Fraction instance by parsing a numeric string.
The value to parse.
[bigint, bigint] representing the successfully parsed Fraction numerator and denominator.
If value is not a valid NumberIsh.
Static
tryTries to parse the given value as a Fraction
The value to parse.
The parsed Fraction value, or undefined if value
is not a valid NumberIsh.
const a = new Fraction('1.23');
const b = Fraction.tryParse('1.23');
a.eq(b); // true
const c = Fraction.tryParse('abc');
c === undefined // true
new Fraction('abc'); // throws
The number of decimal places to expand.
A new Fraction instance representing the expanded fraction.
Use Fraction.shl instead.
Expands the fraction by multiplying it by 10 raised to the specified decimal places.
If n is not a positive integer.
new Fraction(1).expandDecimals(3).eq(1000); // true
new Fraction(1).expandDecimals(4).eq(1000); // true
new Fraction('123').expandDecimals(18).eq(123n * 10n ** 18n); // true
The number of decimal places to normalize.
A new Fraction instance representing the normalized fraction.
Use Fraction.shr instead.
Normalizes the fraction by dividing it by 10 raised to the specified decimal places.
If n is not a positive integer.
new Fraction(1000).normalizeDecimals(3).eq(1); // true
new Fraction(1000).normalizeDecimals(4).eq(0.1); // true
new Fraction('123e18').normalizeDecimals(18).eq(123); // true
(Shift Left) Shift the fraction left by n places.
The number of places to shift the Fraction by.
A new Fraction representing the result of the left shift operation.
If n is not a positive integer.
new Fraction(1).shl(3).eq(1000); // true
new Fraction(1).shl(4).eq(1000); // true
new Fraction('123').expandDecimals(18).eq(123n * 10n ** 18n); // true
(Shift Right) Shift the fraction right by n places.
The number of positions to shift the Fraction by.
A new Fraction representing the result of the right shift operation.
If n is not a positive integer.
new Fraction(1000).shr(3).eq(1); // true
new Fraction(1000).shr(4).eq(0.1); // true
new Fraction('123e18').normalizeDecimals(18).eq(123); // true
Checks if the fraction is equal to other
.
The value to compare with.
True if the fraction is equal to other
, false otherwise.
If other is not a valid NumberIsh
Checks if the fraction is not equal to other
.
The value to compare with.
True if the fraction is not equal to other
, false otherwise.
If other is not a valid NumberIsh
Checks if the fraction is less than other
.
The value to compare with.
True if the fraction is less than other
, false otherwise.
If other is not a valid NumberIsh
Checks if the fraction is less than or equal to other
.
The value to compare with.
True if the fraction is less than or equal to other
, false otherwise.
If other is not a valid NumberIsh
Checks if the fraction is greater than other
.
The value to compare with.
True if the fraction is greater than other
, false otherwise.
If other is not a valid NumberIsh
Checks if the fraction is greater than or equal to other
.
The value to compare with.
True if the fraction is greater than or equal to other
, false otherwise.
If other is not a valid NumberIsh
Adds other
to the fraction.
The value to add.
A new Fraction representing the sum.
If other is not a valid NumberIsh
Subtracts other
from the fraction.
The value to subtract.
A new Fraction representing the difference.
If other is not a valid NumberIsh
Multiplies the fraction by other
.
The value to multiply by.
A new Fraction representing the product.
If other is not a valid NumberIsh
Divides the fraction by other
.
The value to divide by.
A new Fraction representing the quotient.
Converts the fraction to a fixed-point decimal string representation.
Optional
decimalPlaces: numberThe number of decimal places to include. (default: 0)
Optional
opts: { Optional
decimalThe number of decimal places to round to when using the toFormat() method.
Optional
roundingThe rounding mode to be applied.
Optional
trailingDetermines whether trailing zeros are preserved.
Optional
format?: { Formatting options for the Fraction.toFormat method.
Optional
groupThe grouping size of the integer part, default to 3
.
Optional
groupThe grouping separator of the integer part, default to ,
.
Optional
decimalThe decimal separator, default to .
.
The fixed-point decimal string representation of the fraction.
If decimalPlaces
is not a positive integer.
new Fraction('123.567').toFixed(); // '124'
new Fraction('123.567', { roundingMode: RoundingMode.ROUND_HALF_DOWN }).toFixed(); // '123'
new Fraction('123.567').toFixed(1); // '123.6'
new Fraction('123.567').toFixed(2); // '123.57'
new Fraction('123.567', { roundingMode: RoundingMode.ROUND_DOWN } ).toFixed(2); // '123.56'
new Fraction('123.567').toFixed(3); // '123.567'
new Fraction('123.567').toFixed(4); // '123.5670'
new Fraction('123.567').toFixed(5); // '123.56700'
new Fraction('123.567').toFixed(5, { trailingZeros: false }); // '123.567'
Converts the fraction to a string representation with the specified significant digits.
Optional
significantDigits: numberThe number of significant digits in the resulting string representation.
Optional
opts: { Optional
significantThe number of significant digits to preserve when using the toPrecision() method.
Optional
roundingThe rounding mode.
The string representation of the fraction with the specified number of significant digits.
If significantDigits
is not a >= 1 integer.
new Fraction('1234.567').toPrecision(1); // '1000'
new Fraction('1234.567').toPrecision(2); // '1200'
new Fraction('1234.567').toPrecision(3); // '1230'
new Fraction('1234.567').toPrecision(4); // '1235'
new Fraction('1234.567').toPrecision(4, { roundingMode: RoundingMode.ROUND_DOWN }); // '1234'
new Fraction('1234.567').toPrecision(5); // '1234.6'
new Fraction('1234.567').toPrecision(6); // '1234.57'
new Fraction('1234.567').toPrecision(7); // '1234.567'
new Fraction('1234.567').toPrecision(8); // '1234.5670'
new Fraction('1234.567').toPrecision(9); // '1234.56700'
Converts a Fraction to a string of exponential representation.
Optional
decimalPlaces: numberThe number of decimal places in the resulting string.
Optional
opts: { Optional
decimalThe number of decimal places to round to when using the toExponential() method.
Optional
roundingThe rounding mode to be applied.
Optional
trailingDetermines whether trailing zeros are preserved when using the toExponential() method.
The string of exponential representation of the fraction.
If decimalPlaces
is not a positive integer.
new Fraction(0).toExponential() // '0e+0'
new Fraction(0).toExponential(1) // '0.0e+0'
new Fraction('0.0000001234').toExponential(4) // '1.2340e-7'
new Fraction(1234.5678).toExponential(1) // '1.2e+3'
new Fraction(-1234.5678).toExponential(1) // '-1.2e+3'
new Fraction(1234.5678).toExponential(3) // '1.235e+3'
new Fraction(1234.5678).toExponential(5) // '1.23457e+3'
new Fraction(10n ** 100n).toExponential(2) // '1.00e+100'
Converts the fraction to a formatted string representation.
Optional
opts: { Optional
decimalThe number of decimal places to round to when using the toFormat() method.
Optional
roundingThe rounding mode to be applied.
Optional
trailingDetermines whether trailing zeros are preserved.
Optional
format?: { Formatting options for the Fraction.toFormat method.
Optional
groupThe grouping size of the integer part, default to 3
.
Optional
groupThe grouping separator of the integer part, default to ,
.
Optional
decimalThe decimal separator, default to .
.
The formatted string representation of the fraction.
new Fraction('1234.567').toFormat(); // '123,5'
new Fraction('1234.567').toFormat({ decimalPlaces: 1, format: { groupSeparator: '-' } }); // '123-4.6'
new Fraction('1234.567', {}).toFormat({ decimalPlaces: 1 }); // '123,4.6'
new Fraction('1234.567', {}).toFormat({ decimalPlaces: 1, roundingMode: RoundingMode.ROUND_DOWN }); // '123,4.5'
Generated using TypeDoc
Creates a new Fraction instance.