UNPKG

@sctg/scientific-notation

Version:

A TypeScript library for converting numbers to various scientific notation formats

18 lines (17 loc) 2.29 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),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(r){function i(o){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:3;if(o===0)return{mantissa:0,exponent:0};const n=Math.floor(Math.log10(Math.abs(o))),e=Math.floor(n/3)*3,s=o/Math.pow(10,e),u=Math.pow(10,t-1-Math.floor(Math.log10(Math.abs(s))));return{mantissa:Math.round(s*u)/u,exponent:e}}r.toScientificNotation=i;function a(o,t){t===void 0&&(t=3);const n=o.toPrecision(t),e=n.replace(".","").replace("-","").length;return e>t?o.toString():e<t?n.includes(".")?n+"0".repeat(t-e+1):n+"."+"0".repeat(t-e):n}r.toMantissaString=a;function m(o,t){const{mantissa:n,exponent:e}=i(o,t);return e===0?a(n,t):e===1?`${a(n*10,t)}`:`${a(n,t)}e${e}`}r.toScientificNotationString=m;function f(o,t){const{mantissa:n,exponent:e}=i(o,t);return e===0?a(n,t):e===1?`${a(n*10,t)}`:`${a(n,t)} \\times 10^{${e}}`}r.toScientificNotationLatex=f;function c(o,t){const{mantissa:n,exponent:e}=i(o,t);return e===0?`<math><mrow><mn>${a(n,t)}</mn></mrow></math>`:e===1?`<math><mrow><mn>${a(n*10,t)}</mn></mrow></math>`:`<math><mrow><mn>${a(n,t)}</mn><mo>\xD7</mo><msup><mn>10</mn><mn>${e}</mn></msup></mrow></math>`}r.toScientificNotationMathML=c;function l(o,t){const{mantissa:n,exponent:e}=i(o,t);return e===0?a(n,t):e===1?`${a(n*10,t)}`:`${a(n,t)} \xD7 10<sup>${e}</sup>`}r.toScientificNotationHTML=l})(ScientificNotation||(exports.ScientificNotation=ScientificNotation={}));