Class Fraction

Constructors

  • Creates a new Fraction instance.

    Parameters

    • numerator: FractionIsh

      The numerator of the fraction.

    • Optional denominator: FractionIsh

      The denominator of the fraction. (default: 1n)

    Returns Fraction

    Throws

    1. If the numerator or denominator is not a valid NumberIsh.
    2. If denominator is zero.
    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

Properties

ZERO: Fraction = ...
ONE: Fraction = ...
numerator: bigint

The numerator of the fraction.

denominator: bigint

The denominator of the fraction.

_config: Config = DEFAULT_CONFIG

Accessors

  • get quotient(): bigint
  • Gets the quotient of the fraction (the integer part of the division).

    Returns bigint

    The quotient of the fraction.

    new Fraction('123.789').quotient; // 123n
    new Fraction('0.789').quotient; // 0n
  • get remainder(): Fraction
  • Gets the remainder of the fraction as a new Fraction instance.

    Returns Fraction

    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
  • get asFraction(): Fraction
  • Helper method for converting any super class back to a fraction

    Returns Fraction

    A Fraction instance representing the current fraction.

Methods

  • Sets the configuration by merging with the provided partial configuration.

    Parameters

    • c: Partial<Config>

      The partial configuration to merge.

    Returns void

    The updated configuration.

  • Creates a Fraction instance by parsing a numeric string.

    Parameters

    Returns [bigint, bigint]

    [bigint, bigint] representing the successfully parsed Fraction numerator and denominator.

    Throws

    If value is not a valid NumberIsh.

  • Tries to parse the given value as a Fraction

    Parameters

    Returns undefined | Fraction

    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
  • Inverts the fraction by swapping the numerator and denominator.

    Returns Fraction

    The inverted fraction.

    new Fraction('0.5').invert().eq(2); // true
    new Fraction('0.25').invert().eq(4); // true
    new Fraction(1, 3).invert().eq(3); // true
    new Fraction(0).invert().eq(0); // true
  • Negates the sign of the Fraction.

    Returns Fraction

    The Fraction with the sign inverted.

    new Fraction('123').negate().eq('-123'); // true

    const a = new Fraction('123.456');
    const b = new Fraction('-123.456');
    a.negate().eq(b); // true
  • Returns the absolute value of the fraction.

    Returns Fraction

    A new Fraction instance representing the absolute value of the fraction.

    new Fraction('-123').abs().eq('123');
    
  • Parameters

    • decimals: number

      The number of decimal places to expand.

    Returns Fraction

    A new Fraction instance representing the expanded fraction.

    Deprecated

    Use Fraction.shl instead.

    Expands the fraction by multiplying it by 10 raised to the specified decimal places.

    Throws

    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
  • Parameters

    • decimals: number

      The number of decimal places to normalize.

    Returns Fraction

    A new Fraction instance representing the normalized fraction.

    Deprecated

    Use Fraction.shr instead.

    Normalizes the fraction by dividing it by 10 raised to the specified decimal places.

    Throws

    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.

    Parameters

    • n: number

      The number of places to shift the Fraction by.

    Returns Fraction

    A new Fraction representing the result of the left shift operation.

    Throws

    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.

    Parameters

    • n: number

      The number of positions to shift the Fraction by.

    Returns Fraction

    A new Fraction representing the result of the right shift operation.

    Throws

    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 zero.

    Returns boolean

    True if the fraction is zero, false otherwise.

    new Fraction(0).isZero(); // true
    new Fraction(100).isZero(); // false
  • Checks if the fraction is an integer.

    Returns boolean

    True if the fraction is an integer, false otherwise.

    new Fraction(1).isInteger(); // true
    new Fraction(1.1).isInteger(); // false
  • Checks if the fraction is equal to other.

    Parameters

    Returns boolean

    True if the fraction is equal to other, false otherwise.

    Throws

    If other is not a valid NumberIsh

  • Checks if the fraction is not equal to other.

    Parameters

    Returns boolean

    True if the fraction is not equal to other, false otherwise.

    Throws

    If other is not a valid NumberIsh

  • Checks if the fraction is less than other.

    Parameters

    Returns boolean

    True if the fraction is less than other, false otherwise.

    Throws

    If other is not a valid NumberIsh

  • Checks if the fraction is less than or equal to other.

    Parameters

    Returns boolean

    True if the fraction is less than or equal to other, false otherwise.

    Throws

    If other is not a valid NumberIsh

  • Checks if the fraction is greater than other.

    Parameters

    Returns boolean

    True if the fraction is greater than other, false otherwise.

    Throws

    If other is not a valid NumberIsh

  • Checks if the fraction is greater than or equal to other.

    Parameters

    Returns boolean

    True if the fraction is greater than or equal to other, false otherwise.

    Throws

    If other is not a valid NumberIsh

  • Adds other to the fraction.

    Parameters

    Returns Fraction

    A new Fraction representing the sum.

    Throws

    If other is not a valid NumberIsh

  • Subtracts other from the fraction.

    Parameters

    Returns Fraction

    A new Fraction representing the difference.

    Throws

    If other is not a valid NumberIsh

  • Multiplies the fraction by other.

    Parameters

    Returns Fraction

    A new Fraction representing the product.

    Throws

    If other is not a valid NumberIsh

  • Divides the fraction by other.

    Parameters

    Returns Fraction

    A new Fraction representing the quotient.

    Throws

    1. If other is not a valid NumberIsh.
    2. other is zero.
  • Converts the fraction to a fixed-point decimal string representation.

    Parameters

    • Optional decimalPlaces: number

      The number of decimal places to include. (default: 0)

    • Optional opts: {
          decimalPlaces?: number;
          roundingMode?: RoundingMode;
          trailingZeros?: boolean;
          format?: {
              groupSize?: number;
              groupSeparator?: string;
              decimalSeparator?: string;
          };
      }
      • Optional decimalPlaces?: number

        The number of decimal places to round to when using the toFormat() method.

      • Optional roundingMode?: RoundingMode

        The rounding mode to be applied.

      • Optional trailingZeros?: boolean

        Determines whether trailing zeros are preserved.

      • Optional format?: {
            groupSize?: number;
            groupSeparator?: string;
            decimalSeparator?: string;
        }

        Formatting options for the Fraction.toFormat method.

        • Optional groupSize?: number

          The grouping size of the integer part, default to 3.

        • Optional groupSeparator?: string

          The grouping separator of the integer part, default to ,.

        • Optional decimalSeparator?: string

          The decimal separator, default to ..

    Returns string

    The fixed-point decimal string representation of the fraction.

    Throws

    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.

    Parameters

    • Optional significantDigits: number

      The number of significant digits in the resulting string representation.

    • Optional opts: {
          significantDigits?: number;
          roundingMode?: RoundingMode;
      }
      • Optional significantDigits?: number

        The number of significant digits to preserve when using the toPrecision() method.

      • Optional roundingMode?: RoundingMode

        The rounding mode.

    Returns string

    The string representation of the fraction with the specified number of significant digits.

    Throws

    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.

    Parameters

    • Optional decimalPlaces: number

      The number of decimal places in the resulting string.

    • Optional opts: {
          decimalPlaces?: number;
          roundingMode?: RoundingMode;
          trailingZeros?: boolean;
      }
      • Optional decimalPlaces?: number

        The number of decimal places to round to when using the toExponential() method.

      • Optional roundingMode?: RoundingMode

        The rounding mode to be applied.

      • Optional trailingZeros?: boolean

        Determines whether trailing zeros are preserved when using the toExponential() method.

    Returns string

    The string of exponential representation of the fraction.

    Throws

    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.

    Parameters

    • Optional opts: {
          decimalPlaces?: number;
          roundingMode?: RoundingMode;
          trailingZeros?: boolean;
          format?: {
              groupSize?: number;
              groupSeparator?: string;
              decimalSeparator?: string;
          };
      }
      • Optional decimalPlaces?: number

        The number of decimal places to round to when using the toFormat() method.

      • Optional roundingMode?: RoundingMode

        The rounding mode to be applied.

      • Optional trailingZeros?: boolean

        Determines whether trailing zeros are preserved.

      • Optional format?: {
            groupSize?: number;
            groupSeparator?: string;
            decimalSeparator?: string;
        }

        Formatting options for the Fraction.toFormat method.

        • Optional groupSize?: number

          The grouping size of the integer part, default to 3.

        • Optional groupSeparator?: string

          The grouping separator of the integer part, default to ,.

        • Optional decimalSeparator?: string

          The decimal separator, default to ..

    Returns string

    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'
  • Converts the Fraction object to a JSON representation for avoiding serialization errors.

    Returns {
        numerator: string;
        denominator: string;
    }

    • numerator: string
    • denominator: string

Generated using TypeDoc