UNPKG

@js-data-tools/js-helpers

Version:

A set of JavaScript / TypeScript helper functions for parsing, converting, transforming and formatting data.

29 lines (28 loc) 1.37 kB
/** * Rounds the given number to have at most <n> decimal digits. * * @since 0.1.2 * @category convert * @param {number} value - The numeric value to round * @param {number} [maxDecimalDigits=2] - The maximal number of decimal digits to keep. Negative or undefined value means "do not round". * @returns {number} The given number, rounded to the requested number of decimal digits. * @example * * console.log(roundNumber(3.14159265)); // 3.14 * console.log(roundNumber(3.14159265, 4)); // 3.1416 * console.log(roundNumber(-2.246)); // -2.25 * */ export declare function roundNumber(value: number, maxDecimalDigits?: number): number; /** * Converts number to a [compact, power] tuple. In other words, value = compact * base ^ power. * This format allows formatting given number as a compact string (1234567890 => [1.23, 3] => 1.23G) * * @since 0.1.2 * @category convert * @param {number} value The numeric value to convert. * @param {number} [maxPower=4] The maximal allowed power of the base (used to make sure we have a unit name for the power) * @param {number} [base=1000] The base for the power (default is 1000). * @returns {[number, number]} The array, containing two numbers: the compact and power. */ export declare function compactNumber(value: number, maxPower?: number, base?: number): [compact: number, power: number];