UNPKG

@sctg/scientific-notation

Version:

A TypeScript library for converting numbers to various scientific notation formats

125 lines (124 loc) 5.05 kB
/** * @copyright Copyright (c) 2024-2025 Ronan LE MEILLAT * @license AGPL-3.0-or-later * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ export type ScientificNotationNumber = { mantissa: number; exponent: number; }; export declare namespace ScientificNotation { /** * Convert a number to scientific notation * @param value - The number to convert to scientific notation * @param precision - The number of significant figures to keep */ function toScientificNotation(value: number, precision?: number): ScientificNotationNumber; /** * Convert a mantissa number to a string representation with the correct number of significant figures * @param mantissa - The mantissa number to convert to a string * @param precision - The number of significant figures to keep * @returns a string representation of the mantissa with the correct number of significant figures * @example * ```ts * ScientificNotation.toMantissaString(1.23, 4) * // returns "1.230" * ``` * @example * ```ts * ScientificNotation.toMantissaString(123, 4) * // returns "123.0" * ``` * @example * ```ts * ScientificNotation.toMantissaString(123, 3) * // returns "123" * ``` * @example * ```ts * ScientificNotation.toMantissaString(123, 2) * // returns "1.2" * ``` */ function toMantissaString(mantissa: number, precision?: number): string; /** * Convert a number to scientific notation and return a string representation * @param value - The number to convert to scientific notation * @param precision - The number of significant numbers to keep * @returns a string representation of the number in scientific notation * @example * ```ts * ScientificNotation.toScientificNotationString(1234567890.1234567890, 3) * // returns "1.23e9" * ``` * @example * ```ts * ScientificNotation.toScientificNotationString(1230017890.1234567890, 5) * // returns "1.2300e9" * ``` */ function toScientificNotationString(value: number, precision?: number): string; /** * Convert a number to scientific notation and return a LaTeX representation * @param value - The number to convert to scientific notation * @param precision - The number of significant numbers to keep * @returns a LaTeX representation of the number in scientific notation * @example * ```ts * ScientificNotation.toScientificNotationLatex(1234567890.1234567890, 3) * // returns "1.23 \times 10^{9}" * ``` * @example * ```ts * ScientificNotation.toScientificNotationLatex(1230017890.1234567890, 5) * // returns "1.2300 \times 10^{9}" * ``` */ function toScientificNotationLatex(value: number, precision?: number): string; /** * Convert a number to scientific notation and return a MathML representation * @param value - The number to convert to scientific notation * @param precision - The number of significant numbers to keep * @returns a MathML representation of the number in scientific notation * @example * ```ts * ScientificNotation.toScientificNotationMathML(1234567890.1234567890, 3) * // returns "<math><mrow><mn>1.23</mn><mo>×</mo><msup><mn>10</mn><mn>9</mn></msup></mrow></math>" * ``` * @example * ```ts * ScientificNotation.toScientificNotationMathML(1230017890.1234567890, 5) * // returns "<math><mrow><mn>1.2300</mn><mo>×</mo><msup><mn>10</mn><mn>9</mn></msup></mrow></math>" * ``` */ function toScientificNotationMathML(value: number, precision?: number): string; /** * Convert a number to scientific notation and return an HTML representation * @param value - The number to convert to scientific notation * @param precision - The number of significant numbers to keep * @returns an HTML representation of the number in scientific notation * @example * ```ts * ScientificNotation.toScientificNotationHTML(1234567890.1234567890, 3) * // returns "1.23 × 10<sup>9</sup>" * ``` * @example * ```ts * ScientificNotation.toScientificNotationHTML(1230017890.1234567890, 5) * // returns "1.2300 × 10<sup>9</sup>" * ``` */ function toScientificNotationHTML(value: number, precision?: number): string; }