From daa1a2aff136fa9da1fcc97d7da97a2036fabc77 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:51:47 +0100 Subject: unslug uk: move --- .../reference/global_objects/math/round/index.html | 194 +++++++++++++++++++++ .../index.html" | 194 --------------------- 2 files changed, 194 insertions(+), 194 deletions(-) create mode 100644 files/uk/web/javascript/reference/global_objects/math/round/index.html delete mode 100644 "files/uk/web/javascript/reference/global_objects/math/\320\267\320\260\320\276\320\272\321\200\321\203\320\263\320\265\320\273\320\275\320\275\321\217/index.html" (limited to 'files/uk/web/javascript/reference/global_objects/math') diff --git a/files/uk/web/javascript/reference/global_objects/math/round/index.html b/files/uk/web/javascript/reference/global_objects/math/round/index.html new file mode 100644 index 0000000000..92cc055f83 --- /dev/null +++ b/files/uk/web/javascript/reference/global_objects/math/round/index.html @@ -0,0 +1,194 @@ +--- +title: Math.round() +slug: Web/JavaScript/Reference/Global_Objects/Math/заокругелння +tags: + - JavaScript + - Math + - Довідка + - Заокруглення + - метод +translation_of: Web/JavaScript/Reference/Global_Objects/Math/round +--- +
{{JSRef}}
+ +

Функція Math.round() повертає значення числа заокруглене до найближчого цілого.

+ +

 

+ +
{{EmbedInteractiveExample("pages/js/math-round.html")}}
+ + + +

 

+ +

Синтаксис

+ +
Math.round(x)
+ +

Параметри

+ +
+
x
+
Число.
+
+ +

Повернуте значення

+ +

Значення отриманого числа, округлене до найближчого цілого.

+ +

Опис

+ +

Якщо дробова частина числа більша або рівна 0.5, аргумент буде округлено до найближчого більшого цілого. Якщо дробова частина числа менша 0.5, аргумент буде округлено до найближчого меншого цілого. Якщо дробова частина рівна 0.5, то аргумент буде округлено до найближчого цілого в напрямку +∞.  Зверніть увагу, що функція round() відрізняється від багатьох мов, які часто приводять цей випадок до наступного цілого числа, віддаленого від нуля (маємо інший результат у випадку від'ємних чисел з дробовою частиною рівною 0.5).

+ +

Оскільки round() - це статичний метод Math, ви завжди використовуєте його як Math.round (), а не як метод об'єкта Math, який ви створили (Math не має конструктора).

+ +

Приклади

+ +
Math.round( 20.49); //  20
+Math.round( 20.5);  //  21
+Math.round( 42  );  //  42
+Math.round(-20.5);  // -20
+Math.round(-20.51); // -21
+
+ +

Десяткове округлення

+ +
// Closure
+(function() {
+  /**
+   * Decimal adjustment of a number.
+   *
+   * @param {String}  type  The type of adjustment.
+   * @param {Number}  value The number.
+   * @param {Integer} exp   The exponent (the 10 logarithm of the adjustment base).
+   * @returns {Number} The adjusted value.
+   */
+  function decimalAdjust(type, value, exp) {
+    // If the exp is undefined or zero...
+    if (typeof exp === 'undefined' || +exp === 0) {
+      return Math[type](value);
+    }
+    value = +value;
+    exp = +exp;
+    // If the value is not a number or the exp is not an integer...
+    if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
+      return NaN;
+    }
+    // If the value is negative...
+    if (value < 0) {
+      return -decimalAdjust(type, -value, exp);
+    }
+    // Shift
+    value = value.toString().split('e');
+    value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
+    // Shift back
+    value = value.toString().split('e');
+    return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
+  }
+
+  // Decimal round
+  if (!Math.round10) {
+    Math.round10 = function(value, exp) {
+      return decimalAdjust('round', value, exp);
+    };
+  }
+  // Decimal floor
+  if (!Math.floor10) {
+    Math.floor10 = function(value, exp) {
+      return decimalAdjust('floor', value, exp);
+    };
+  }
+  // Decimal ceil
+  if (!Math.ceil10) {
+    Math.ceil10 = function(value, exp) {
+      return decimalAdjust('ceil', value, exp);
+    };
+  }
+})();
+
+// Round
+Math.round10(55.55, -1);   // 55.6
+Math.round10(55.549, -1);  // 55.5
+Math.round10(55, 1);       // 60
+Math.round10(54.9, 1);     // 50
+Math.round10(-55.55, -1);  // -55.5
+Math.round10(-55.551, -1); // -55.6
+Math.round10(-55, 1);      // -50
+Math.round10(-55.1, 1);    // -60
+Math.round10(1.005, -2);   // 1.01 -- compare this with Math.round(1.005*100)/100 above
+Math.round10(-1.005, -2);  // -1.01
+// Floor
+Math.floor10(55.59, -1);   // 55.5
+Math.floor10(59, 1);       // 50
+Math.floor10(-55.51, -1);  // -55.6
+Math.floor10(-51, 1);      // -60
+// Ceil
+Math.ceil10(55.51, -1);    // 55.6
+Math.ceil10(51, 1);        // 60
+Math.ceil10(-55.59, -1);   // -55.5
+Math.ceil10(-59, 1);       // -50
+
+ +

PHP-Like rounding Method

+ +

The code below can be used to add your own version of Math.round to your own namespace which takes a precision parameter. Unlike Decimal rounding in the example above, this performs no conversion to and from strings, and the precision parameter works same way as PHP and Excel whereby a positive 1 would round to 1 decimal place and -1 would round to the tens.

+ +
var myNamespace = {};
+
+myNamespace.round = function(number, precision) {
+    var factor = Math.pow(10, precision);
+    var tempNumber = number * factor;
+    var roundedTempNumber = Math.round(tempNumber);
+    return roundedTempNumber / factor;
+};
+
+myNamespace.round(1234.5678, 1); // 1234.6
+myNamespace.round(1234.5678, -1); // 1230
+ +

Cпецифікації

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
СпецифікаціяСтатусКоментар
{{SpecName('ES1')}}{{Spec2('ES1')}}Початкове визначення. Впроваджено в JavaScript 1.0.
{{SpecName('ES5.1', '#sec-15.8.2.15', 'Math.round')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-math.round', 'Math.round')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-math.round', 'Math.round')}}{{Spec2('ESDraft')}} 
+ +

Сумісність браузерів

+ + + +

{{Compat("javascript.builtins.Math.round")}}

+ +

Дивіться також

+ + diff --git "a/files/uk/web/javascript/reference/global_objects/math/\320\267\320\260\320\276\320\272\321\200\321\203\320\263\320\265\320\273\320\275\320\275\321\217/index.html" "b/files/uk/web/javascript/reference/global_objects/math/\320\267\320\260\320\276\320\272\321\200\321\203\320\263\320\265\320\273\320\275\320\275\321\217/index.html" deleted file mode 100644 index 92cc055f83..0000000000 --- "a/files/uk/web/javascript/reference/global_objects/math/\320\267\320\260\320\276\320\272\321\200\321\203\320\263\320\265\320\273\320\275\320\275\321\217/index.html" +++ /dev/null @@ -1,194 +0,0 @@ ---- -title: Math.round() -slug: Web/JavaScript/Reference/Global_Objects/Math/заокругелння -tags: - - JavaScript - - Math - - Довідка - - Заокруглення - - метод -translation_of: Web/JavaScript/Reference/Global_Objects/Math/round ---- -
{{JSRef}}
- -

Функція Math.round() повертає значення числа заокруглене до найближчого цілого.

- -

 

- -
{{EmbedInteractiveExample("pages/js/math-round.html")}}
- - - -

 

- -

Синтаксис

- -
Math.round(x)
- -

Параметри

- -
-
x
-
Число.
-
- -

Повернуте значення

- -

Значення отриманого числа, округлене до найближчого цілого.

- -

Опис

- -

Якщо дробова частина числа більша або рівна 0.5, аргумент буде округлено до найближчого більшого цілого. Якщо дробова частина числа менша 0.5, аргумент буде округлено до найближчого меншого цілого. Якщо дробова частина рівна 0.5, то аргумент буде округлено до найближчого цілого в напрямку +∞.  Зверніть увагу, що функція round() відрізняється від багатьох мов, які часто приводять цей випадок до наступного цілого числа, віддаленого від нуля (маємо інший результат у випадку від'ємних чисел з дробовою частиною рівною 0.5).

- -

Оскільки round() - це статичний метод Math, ви завжди використовуєте його як Math.round (), а не як метод об'єкта Math, який ви створили (Math не має конструктора).

- -

Приклади

- -
Math.round( 20.49); //  20
-Math.round( 20.5);  //  21
-Math.round( 42  );  //  42
-Math.round(-20.5);  // -20
-Math.round(-20.51); // -21
-
- -

Десяткове округлення

- -
// Closure
-(function() {
-  /**
-   * Decimal adjustment of a number.
-   *
-   * @param {String}  type  The type of adjustment.
-   * @param {Number}  value The number.
-   * @param {Integer} exp   The exponent (the 10 logarithm of the adjustment base).
-   * @returns {Number} The adjusted value.
-   */
-  function decimalAdjust(type, value, exp) {
-    // If the exp is undefined or zero...
-    if (typeof exp === 'undefined' || +exp === 0) {
-      return Math[type](value);
-    }
-    value = +value;
-    exp = +exp;
-    // If the value is not a number or the exp is not an integer...
-    if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
-      return NaN;
-    }
-    // If the value is negative...
-    if (value < 0) {
-      return -decimalAdjust(type, -value, exp);
-    }
-    // Shift
-    value = value.toString().split('e');
-    value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
-    // Shift back
-    value = value.toString().split('e');
-    return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
-  }
-
-  // Decimal round
-  if (!Math.round10) {
-    Math.round10 = function(value, exp) {
-      return decimalAdjust('round', value, exp);
-    };
-  }
-  // Decimal floor
-  if (!Math.floor10) {
-    Math.floor10 = function(value, exp) {
-      return decimalAdjust('floor', value, exp);
-    };
-  }
-  // Decimal ceil
-  if (!Math.ceil10) {
-    Math.ceil10 = function(value, exp) {
-      return decimalAdjust('ceil', value, exp);
-    };
-  }
-})();
-
-// Round
-Math.round10(55.55, -1);   // 55.6
-Math.round10(55.549, -1);  // 55.5
-Math.round10(55, 1);       // 60
-Math.round10(54.9, 1);     // 50
-Math.round10(-55.55, -1);  // -55.5
-Math.round10(-55.551, -1); // -55.6
-Math.round10(-55, 1);      // -50
-Math.round10(-55.1, 1);    // -60
-Math.round10(1.005, -2);   // 1.01 -- compare this with Math.round(1.005*100)/100 above
-Math.round10(-1.005, -2);  // -1.01
-// Floor
-Math.floor10(55.59, -1);   // 55.5
-Math.floor10(59, 1);       // 50
-Math.floor10(-55.51, -1);  // -55.6
-Math.floor10(-51, 1);      // -60
-// Ceil
-Math.ceil10(55.51, -1);    // 55.6
-Math.ceil10(51, 1);        // 60
-Math.ceil10(-55.59, -1);   // -55.5
-Math.ceil10(-59, 1);       // -50
-
- -

PHP-Like rounding Method

- -

The code below can be used to add your own version of Math.round to your own namespace which takes a precision parameter. Unlike Decimal rounding in the example above, this performs no conversion to and from strings, and the precision parameter works same way as PHP and Excel whereby a positive 1 would round to 1 decimal place and -1 would round to the tens.

- -
var myNamespace = {};
-
-myNamespace.round = function(number, precision) {
-    var factor = Math.pow(10, precision);
-    var tempNumber = number * factor;
-    var roundedTempNumber = Math.round(tempNumber);
-    return roundedTempNumber / factor;
-};
-
-myNamespace.round(1234.5678, 1); // 1234.6
-myNamespace.round(1234.5678, -1); // 1230
- -

Cпецифікації

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
СпецифікаціяСтатусКоментар
{{SpecName('ES1')}}{{Spec2('ES1')}}Початкове визначення. Впроваджено в JavaScript 1.0.
{{SpecName('ES5.1', '#sec-15.8.2.15', 'Math.round')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-math.round', 'Math.round')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-math.round', 'Math.round')}}{{Spec2('ESDraft')}} 
- -

Сумісність браузерів

- - - -

{{Compat("javascript.builtins.Math.round")}}

- -

Дивіться також

- - -- cgit v1.2.3-54-g00ecf