Add content selection before removing content
[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-2018 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 * @module WoltLabSuite/Core/NumberUtil
8 */
9 define([], function() {
10 "use strict";
11
12 /**
13 * @exports WoltLabSuite/Core/NumberUtil
14 */
15 var NumberUtil = {
16 /**
17 * Decimal adjustment of a number.
18 *
19 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
20 * @param {Number} value The number.
21 * @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
22 * @returns {Number} The adjusted value.
23 */
24 round: function (value, exp) {
25 // If the exp is undefined or zero...
26 if (typeof exp === 'undefined' || +exp === 0) {
27 return Math.round(value);
28 }
29 value = +value;
30 exp = +exp;
31
32 // If the value is not a number or the exp is not an integer...
33 if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
34 return NaN;
35 }
36
37 // Shift
38 value = value.toString().split('e');
39 value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
40
41 // Shift back
42 value = value.toString().split('e');
43 return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
44 }
45 };
46
47 return NumberUtil;
48 });