import FloatToolkit from "@float-toolkit/core";
const ft = new FloatToolkit(10, { forceUseDefaultPrecision: true });
The precision (number of decimals) to use if not specified in the function itself. Can be changed later. Default value if 10.
An optional configuration object.
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.
The options object for this FloatToolkit.
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
.
The rounded sum
ft.add([0.1, 0.2]); // 0.3 if using dynamic precision, or if the precision is set to 16 or lower.
The numbers to add.
Optional
precision: _PrecisionThe precision to use instead of the one chosen by the function.
Divides two or more numbers (starting with the second number, all of the numbers divide the first one), and rounds the result.
The rounded result
ft.divide([0.09, 0.9]); // 0.1 if the precision is set to 16 or lower.
The numbers to divide.
The precision to use instead of the one chosen by the function.
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
.
The rounded product
ft.multiply([0.1, 0.9]); // 0.09 if using dynamic precision, or if the precision is set to 16 or lower.
The numbers to multiply.
Optional
precision: _PrecisionThe precision to use instead of the one chosen by the function.
Resets the options object for this FloatToolkit to its default values and returns the new object.
The new options object.
ft.resetOptions({ forceUseDefaultPrecision: true }); // Default options with forceUseDefaultPrecision set to true.
An optional configuration object containing new options to apply after resetting.
Rounds a number to the specified precision, or to the FloatToolkit's default precision.
The rounded number.
ft.round(0.1 + 0.2); // 0.3 if the precision is set to 16 or lower.
The number to round.
The precision to use. If not specified, the FloatToolkit's default precision is used instead.
Changes the values in the options object for this FloatToolkit and returns the new object.
The new options object.
ft.setOptions({ forceUseDefaultPrecision: true }); // Previous options with forceUseDefaultPrecision set to true.
The configuration object that contains the new options to apply.
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
.
The rounded difference
ft.subtract([0.8, 0.1, 0.3]); // 0.4 if using dynamic precision, or if the precision is set to 15 or lower.
The numbers to subtract.
Optional
precision: _PrecisionThe precision to use instead of the one chosen by the function.
Generated using TypeDoc
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.