95dceb2089c19bdc875a03c1274dcbdca07d7eb1
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / NumberUtil.js
1 /**
2 * Provides helper functions for Number handling.
3 *
4 * @author Tim Duesterhus
5 * @copyright 2001-2022 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 * @deprecated 6.0 All functionality in this module is deprecated.
8 */
9 define(["require", "exports"], function (require, exports) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 exports.round = void 0;
13 /**
14 * @deprecated 6.0 Use `Number.toLocaleString()` or `Number.toFixed()` as appropriate.
15 */
16 function round(value, exp) {
17 // If the exp is undefined or zero...
18 if (typeof exp === "undefined" || +exp === 0) {
19 return Math.round(value);
20 }
21 value = +value;
22 exp = +exp;
23 // If the value is not a number or the exp is not an integer...
24 if (isNaN(value) || !(typeof exp === "number" && exp % 1 === 0)) {
25 return NaN;
26 }
27 // Shift
28 let tmp = value.toString().split("e");
29 let exponent = tmp[1] ? +tmp[1] - exp : -exp;
30 value = Math.round(+`${tmp[0]}e${exponent}`);
31 // Shift back
32 tmp = value.toString().split("e");
33 exponent = tmp[1] ? +tmp[1] + exp : exp;
34 return +`${tmp[0]}e${exponent}`;
35 }
36 exports.round = round;
37 });