Class FloatToolkit

A FloatToolkit contains methods to round floats and perform accurate math operations with them. Its behavior can be configured to cover the program's needs.

Hierarchy

  • FloatToolkit

Constructors

  • Example

    import FloatToolkit from "@float-toolkit/core";
    const ft = new FloatToolkit(10, { forceUseDefaultPrecision: true });

    Parameters

    • defaultPrecision: _Precision = 10

      The precision (number of decimals) to use if not specified in the function itself. Can be changed later. Default value if 10.

    • options: Partial<FloatToolkit.Options> = {}

      An optional configuration object.

    Returns FloatToolkit

Accessors

  • get defaultPrecision(): _Precision
  • An integer between 1 and 17. Defines the precision (number of decimals) to use by default, if the precision is not specified in the function itself.

    Returns _Precision

  • set defaultPrecision(precision: _Precision): void
  • Parameters

    • precision: _Precision

    Returns void

Methods

  • Adds two or more numbers, and rounds the sum.

    NOTE: By default, this method does not use the FloatToolkit's default precision. Instead, it calculates the best precision to use based on the original numbers (dynamic precision). To always use the default precision, set the forceUseDefaultPrecision option to true.

    Returns

    The rounded sum

    Example

    ft.add([0.1, 0.2]); // 0.3 if using dynamic precision, or if the precision is set to 16 or lower.
    

    Parameters

    • numbers: number[]

      The numbers to add.

    • Optional precision: _Precision

      The precision to use instead of the one chosen by the function.

    Returns number

  • Divides two or more numbers (starting with the second number, all of the numbers divide the first one), and rounds the result.

    Returns

    The rounded result

    Example

    ft.divide([0.09, 0.9]); // 0.1 if the precision is set to 16 or lower.
    

    Parameters

    • numbers: number[]

      The numbers to divide.

    • precision: _Precision = ...

      The precision to use instead of the one chosen by the function.

    Returns number

  • Multiplies two or more numbers, and rounds the product.

    NOTE: By default, this method does not use the FloatToolkit's default precision. Instead, it calculates the best precision to use based on the original numbers (dynamic precision). To always use the default precision, set the forceUseDefaultPrecision option to true.

    Returns

    The rounded product

    Example

    ft.multiply([0.1, 0.9]); // 0.09 if using dynamic precision, or if the precision is set to 16 or lower.
    

    Parameters

    • numbers: number[]

      The numbers to multiply.

    • Optional precision: _Precision

      The precision to use instead of the one chosen by the function.

    Returns number

  • Resets the options object for this FloatToolkit to its default values and returns the new object.

    Returns

    The new options object.

    Example

    ft.resetOptions({ forceUseDefaultPrecision: true }); // Default options with forceUseDefaultPrecision set to true.
    

    Parameters

    • options: Partial<FloatToolkit.Options> = {}

      An optional configuration object containing new options to apply after resetting.

    Returns Readonly<FloatToolkit.Options>

  • Rounds a number to the specified precision, or to the FloatToolkit's default precision.

    Returns

    The rounded number.

    Example

    ft.round(0.1 + 0.2); // 0.3 if the precision is set to 16 or lower.
    

    Parameters

    • n: number

      The number to round.

    • precision: _Precision = ...

      The precision to use. If not specified, the FloatToolkit's default precision is used instead.

    Returns number

  • Changes the values in the options object for this FloatToolkit and returns the new object.

    Returns

    The new options object.

    Example

    ft.setOptions({ forceUseDefaultPrecision: true }); // Previous options with forceUseDefaultPrecision set to true.
    

    Parameters

    • options: Partial<FloatToolkit.Options>

      The configuration object that contains the new options to apply.

    Returns Readonly<FloatToolkit.Options>

  • Subtracts two or more numbers (starting with the second number, all of the numbers are subtracted from the first one), and rounds the difference.

    NOTE: By default, this method does not use the FloatToolkit's default precision. Instead, it calculates the best precision to use based on the original numbers (dynamic precision). To always use the default precision, set the forceUseDefaultPrecision option to true.

    Returns

    The rounded difference

    Example

    ft.subtract([0.8, 0.1, 0.3]); // 0.4 if using dynamic precision, or if the precision is set to 15 or lower.
    

    Parameters

    • numbers: number[]

      The numbers to subtract.

    • Optional precision: _Precision

      The precision to use instead of the one chosen by the function.

    Returns number

Generated using TypeDoc