aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/math/trunc/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/math/trunc/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/math/trunc/index.html116
1 files changed, 116 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/math/trunc/index.html b/files/zh-cn/web/javascript/reference/global_objects/math/trunc/index.html
new file mode 100644
index 0000000000..f260b85c58
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/math/trunc/index.html
@@ -0,0 +1,116 @@
+---
+title: Math.trunc()
+slug: Web/JavaScript/Reference/Global_Objects/Math/trunc
+tags:
+ - ECMAScript 2015
+ - JavaScript
+ - Math
+ - Method
+translation_of: Web/JavaScript/Reference/Global_Objects/Math/trunc
+---
+<div>{{JSRef}}</div>
+
+<p><code><strong>Math.trunc()</strong></code> 方法会将数字的小数部分去掉,只保留整数部分。</p>
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="syntaxbox">Math.trunc(<em>value</em>)</pre>
+
+<h3 id="Parameters" name="Parameters">参数</h3>
+
+<dl>
+ <dt><code>value</code></dt>
+ <dd>任意数字</dd>
+</dl>
+
+<h3 id="返回值">返回值</h3>
+
+<p>给定数字的整数部分</p>
+
+<h2 id="Description" name="Description">描述</h2>
+
+<p>不像 <code>Math</code> 的其他三个方法: {{jsxref("Math.floor()")}}、{{jsxref("Math.ceil()")}}、{{jsxref("Math.round()")}} ,<code>Math.trunc()</code> 的执行逻辑很简单,仅仅是<strong>删除</strong>掉数字的小数部分和小数点,不管参数是正数还是负数。</p>
+
+<p>传入该方法的参数会被隐式转换成数字类型。</p>
+
+<p>因为 <code>trunc()</code> 是 <code>Math</code> 对象的静态方法,你必须用 <code>Math.trunc()</code> 来使用,而不是调用你创建的 <code>Math</code> 对象的一个实例方法(<code>Math</code> 没有构造函数)</p>
+
+<h2 id="Examples" name="Examples">示例</h2>
+
+<pre class="brush:js">Math.trunc(13.37) // 13
+Math.trunc(42.84) // 42
+Math.trunc(0.123) // 0
+Math.trunc(-0.123) // -0
+Math.trunc("-1.123") // -1
+Math.trunc(NaN) // NaN
+Math.trunc("foo") // NaN
+Math.trunc() // NaN</pre>
+
+<h2 id="Polyfill">Polyfill</h2>
+
+<pre><code>if (!Math.trunc) {
+ Math.trunc = function(v) {
+ v = +v;
+ if (!isFinite(v)) return v;
+
+ return (v - v % 1) || (v &lt; 0 ? -0 : v === 0 ? v : 0);
+
+ // 返回:
+ // 0 -&gt; 0
+ // -0 -&gt; -0
+ // 0.2 -&gt; 0
+ // -0.2 -&gt; -0
+ // 0.7 -&gt; 0
+ // -0.7 -&gt; -0
+ // Infinity -&gt; Infinity
+ // -Infinity -&gt; -Infinity
+ // NaN -&gt; NaN
+ // null -&gt; 0
+ };
+}</code>
+</pre>
+
+<p>或:</p>
+
+<pre><code>if (!Math.trunc) {
+ Math.trunc = function(v) {
+ v = +v;
+ return (v - v % 1) || (!isFinite(v) || v === 0 ? v : v &lt; 0 ? -0 : 0);
+ };
+}</code></pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范名称</th>
+ <th scope="col">规范状态</th>
+ <th scope="col">备注</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES2015', '#sec-math.trunc', 'Math.trunc')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-math.trunc', 'Math.trunc')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<p>{{Compat("javascript.builtins.Math.trunc")}}</p>
+
+<p><strong style="color: #4d4e53; font-size: 2.14286rem; font-weight: 700; letter-spacing: -1px;">相关链接</strong></p>
+
+<ul>
+ <li>{{jsxref("Math.abs()")}}</li>
+ <li>{{jsxref("Math.ceil()")}}</li>
+ <li>{{jsxref("Math.floor()")}}</li>
+ <li>{{jsxref("Math.round()")}}</li>
+ <li>{{jsxref("Math.sign()")}}</li>
+</ul>