Math.round()

  • Revision slug: JavaScript/Reference/Global_Objects/Math/round
  • Revision title: Math.round
  • Revision id: 332593
  • Created:
  • Creator: Webmonkey
  • Is current revision? No
  • Comment Math.abs link target

Revision Content

Summary

Returns the value of a number rounded to the nearest integer.

Method of Math
Implemented in JavaScript 1.0
ECMAScript Edition ECMAScript 1st Edition

Syntax

Math.round(x) 

Parameters

x
A number.

Description

If the fractional portion of number is .5 or greater, the argument is rounded to the next higher integer. If the fractional portion of number is less than .5, the argument is rounded to the next lower integer.

Because round is a static method of Math, you always use it as Math.round(), rather than as a method of a Math object you created.

Examples

Example: Using Math.round

// Returns the value 20
x = Math.round(20.49);

// Returns the value 21
x = Math.round(20.5);

// Returns the value -20
x = Math.round(-20.5);

// Returns the value -21
x = Math.round(-20.51)

See slso

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns the value of a number rounded to the nearest integer.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Math"><code>Math</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.0</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 1st Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>Math.round(<em>x</em>) </code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>x</code></dt>
  <dd>
    A number.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p>If the fractional portion of <code>number</code> is .5 or greater, the argument is rounded to the next higher integer. If the fractional portion of <code>number</code> is less than .5, the argument is rounded to the next lower integer.</p>
<p>Because <code>round</code> is a static method of <code>Math</code>, you always use it as <code>Math.round()</code>, rather than as a method of a <code>Math</code> object you created.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_Math.round" name="Example:_Using_Math.round">Example: Using <code>Math.round</code></h3>
<pre class="brush:js">
// Returns the value 20
x = Math.round(20.49);

// Returns the value 21
x = Math.round(20.5);

// Returns the value -20
x = Math.round(-20.5);

// Returns the value -21
x = Math.round(-20.51)</pre>
<h2 id="See_also" name="See_also">See slso</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Math/abs">Math.abs</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Math/ceil">Math.ceil</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Math/floor">Math.floor</a></li>
</ul>
<!-- languages({
	"fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Math/round"
}) -->
Revert to this revision