blob: f5504c21af4ed170859b6baef84bb245e4b60a69 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
---
title: Math.round()
slug: Web/JavaScript/Reference/Global_Objects/Math/round
tags:
- JavaScript
- Math
- Method
- Number
- Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Math/round
---
<div>{{JSRef}}</div>
<p><strong><code>Math.round()</code></strong> 関数は、引数として与えた数を四捨五入して、もっとも近似の整数を返します。</p>
<div>{{EmbedInteractiveExample("pages/js/math-round.html")}}</div>
<p class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</p>
<h2 id="Syntax" name="Syntax">構文</h2>
<pre class="syntaxbox notranslate">Math.round(<var>x</var>)</pre>
<h3 id="Parameters" name="Parameters">引数</h3>
<dl>
<dt><code><var>x</var></code></dt>
<dd>数値</dd>
</dl>
<h3 id="Return_value" name="Return_value">返値</h3>
<p>与えられた値をもっとも近似の整数に四捨五入した値。</p>
<h2 id="Description" name="Description">解説</h2>
<p>引数の小数部分が 0.5 以上の場合、その引数は、次に大きい整数に切り上げられます。引数の小数部分が 0.5 未満の場合、その引数は、次に小さい整数に切り下げられます。小数部分が 0.5 である場合は、正の無限大の方向で次の整数に丸められます。<strong>これは多くの言語の <code>round()</code> 関数と異なることに注意してください。この場合はたいてい、<em>0 から遠ざかる</em>次の整数に丸められます</strong> (小数部分が 0.5 である負の値を四捨五入する場合に、結果が変わります)。</p>
<p><code>round()</code> は <code>Math</code> オブジェクトの静的なメソッドなので、自ら生成した <code>Math</code> オブジェクトのメソッドとしてではなく、常に、<code>Math.round()</code> として使用するようにしてください (<code>Math</code> のコンストラクターはありません)。</p>
<h2 id="Examples" name="Examples">例</h2>
<h3 id="Using_round" name="Using_round">round の使用</h3>
<pre class="brush: js notranslate">Math.round( 20.49); // 20
Math.round( 20.5 ); // 21
Math.round( 42 ); // 42
Math.round(-20.5 ); // -20
Math.round(-20.51); // -21</pre>
<h2 id="Specifications" name="Specifications">仕様書</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">仕様書</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-math.round', 'Math.round')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
<p class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</p>
<p>{{Compat("javascript.builtins.Math.round")}}</p>
<h2 id="See_also" name="See_also">関連情報</h2>
<ul>
<li>{{jsxref("Number.toPrecision()")}}</li>
<li>{{jsxref("Number.toFixed()")}}</li>
<li>{{jsxref("Math.abs()")}}</li>
<li>{{jsxref("Math.ceil()")}}</li>
<li>{{jsxref("Math.floor()")}}</li>
<li>{{jsxref("Math.sign()")}}</li>
<li>{{jsxref("Math.trunc()")}}</li>
</ul>
|