@sctg/scientific-notation
Version:
A TypeScript library for converting numbers to various scientific notation formats
129 lines (125 loc) • 4.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ScientificNotation = void 0;
/**
* @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/>.
*/
let ScientificNotation = exports.ScientificNotation = void 0;
(function (_ScientificNotation) {
function toScientificNotation(value) {
let precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
if (value === 0) {
return {
mantissa: 0,
exponent: 0
};
}
// Calculate the exposure standardized by steps of 3
const exp = Math.floor(Math.log10(Math.abs(value)));
const normalizedExp = Math.floor(exp / 3) * 3;
// Calculate the mantissa
const mantissa = value / Math.pow(10, normalizedExp);
// Adjust precision to have the good number of significant figures
const factor = Math.pow(10, precision - 1 - Math.floor(Math.log10(Math.abs(mantissa))));
const roundedMantissa = Math.round(mantissa * factor) / factor;
return {
mantissa: roundedMantissa,
exponent: normalizedExp
};
}
_ScientificNotation.toScientificNotation = toScientificNotation;
function toMantissaString(mantissa, precision) {
if (precision === undefined) {
precision = 3;
}
const mantissaString = mantissa.toPrecision(precision);
// Compute the total number of digits
const totalDigits = mantissaString.replace(".", "").replace("-", "").length;
if (totalDigits > precision) {
return mantissa.toString();
}
// if totalDigits is less than precision, add zeros
if (totalDigits < precision) {
if (mantissaString.includes(".")) {
return mantissaString + "0".repeat(precision - totalDigits + 1);
} else {
return mantissaString + "." + "0".repeat(precision - totalDigits);
}
}
return mantissaString;
}
_ScientificNotation.toMantissaString = toMantissaString;
function toScientificNotationString(value, precision) {
const {
mantissa,
exponent
} = toScientificNotation(value, precision);
if (exponent === 0) {
return toMantissaString(mantissa, precision);
} else if (exponent === 1) {
return `${toMantissaString(mantissa * 10, precision)}`;
} else {
return `${toMantissaString(mantissa, precision)}e${exponent}`;
}
}
_ScientificNotation.toScientificNotationString = toScientificNotationString;
function toScientificNotationLatex(value, precision) {
const {
mantissa,
exponent
} = toScientificNotation(value, precision);
if (exponent === 0) {
return toMantissaString(mantissa, precision);
} else if (exponent === 1) {
return `${toMantissaString(mantissa * 10, precision)}`;
} else {
return `${toMantissaString(mantissa, precision)} \\times 10^{${exponent}}`;
}
}
_ScientificNotation.toScientificNotationLatex = toScientificNotationLatex;
function toScientificNotationMathML(value, precision) {
const {
mantissa,
exponent
} = toScientificNotation(value, precision);
if (exponent === 0) {
return `<math><mrow><mn>${toMantissaString(mantissa, precision)}</mn></mrow></math>`;
} else if (exponent === 1) {
return `<math><mrow><mn>${toMantissaString(mantissa * 10, precision)}</mn></mrow></math>`;
} else {
return `<math><mrow><mn>${toMantissaString(mantissa, precision)}</mn><mo>×</mo><msup><mn>10</mn><mn>${exponent}</mn></msup></mrow></math>`;
}
}
_ScientificNotation.toScientificNotationMathML = toScientificNotationMathML;
function toScientificNotationHTML(value, precision) {
const {
mantissa,
exponent
} = toScientificNotation(value, precision);
if (exponent === 0) {
return toMantissaString(mantissa, precision);
} else if (exponent === 1) {
return `${toMantissaString(mantissa * 10, precision)}`;
} else {
return `${toMantissaString(mantissa, precision)} × 10<sup>${exponent}</sup>`;
}
}
_ScientificNotation.toScientificNotationHTML = toScientificNotationHTML;
})(ScientificNotation || (exports.ScientificNotation = ScientificNotation = {}));
//# sourceMappingURL=scientific.js.map