Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 797851 of Math.fround()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Math/fround
  • Revision title: Math.fround()
  • Revision id: 797851
  • Created:
  • Creator: xfq
  • Is current revision? No
  • Comment Change HTTP to HTTPS to avoid plaintext HTTP connections.

Revision Content

{{JSRef("Global_Objects", "Math")}} {{harmony}}

Summary

The Math.fround() function returns the nearest single precision float representation of a number.

Syntax

Math.fround(x)

Parameters

x
A number.

Description

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

Examples

Example: Using Math.fround()

Math.fround(0);     // 0
Math.fround(1);     // 1
Math.fround(1.337); // 1.3370000123977661
Math.fround(1.5);   // 1.5
Math.fround(NaN);   // NaN

Polyfill

This can be emulated with the following function, if {{jsxref("Float32Array")}} are supported:

Math.fround = Math.fround || function(x) {
  return new Float32Array([x])[0];
};

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-math.fround', 'Math.fround')}} {{Spec2('ES6')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome("38")}} {{CompatGeckoDesktop("26")}} {{CompatNo}} {{CompatOpera("25")}} {{CompatSafari("7.1")}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatNo}} {{CompatNo}} iOS 8

See also

  • {{jsxref("Math.round()")}}

Revision Source

<div>{{JSRef("Global_Objects", "Math")}} {{harmony}}</div>

<h2 id="Summary" name="Summary">Summary</h2>

<p>The <strong><code>Math.fround()</code></strong> function returns the nearest <a class="external" href="https://en.wikipedia.org/wiki/Single-precision_floating-point_format" title="link to the wikipedia page on single-precision floating-point format">single precision</a> float representation of a number.</p>

<h2 id="Syntax" name="Syntax">Syntax</h2>

<pre class="syntaxbox">
<code>Math.fround(<var>x</var>)</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>Because <code>fround()</code> is a static method of <code>Math</code>, you always use it as <code>Math.fround()</code>, rather than as a method of a <code>Math</code> object you created (<code>Math</code> is not a constructor).</p>

<h2 id="Examples" name="Examples">Examples</h2>

<h3 id="Example:_Using_Math.fround" name="Example:_Using_Math.fround">Example: Using <code>Math.fround()</code></h3>

<pre class="brush: js">
Math.fround(0);     // 0
Math.fround(1);     // 1
Math.fround(1.337); // 1.3370000123977661
Math.fround(1.5);   // 1.5
Math.fround(NaN);   // NaN
</pre>

<h2 id="Polyfill" name="Polyfill">Polyfill</h2>

<p>This can be emulated with the following function, if {{jsxref("Float32Array")}} are supported:</p>

<pre class="brush: js">
Math.fround = Math.fround || function(x) {
  return new Float32Array([x])[0];
};
</pre>

<h2 id="Specifications" name="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-math.fround', 'Math.fround')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome("38")}}</td>
   <td>{{CompatGeckoDesktop("26")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatOpera("25")}}</td>
   <td>{{CompatSafari("7.1")}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>iOS 8</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also" name="See_also">See also</h2>

<ul>
 <li>{{jsxref("Math.round()")}}</li>
</ul>
Revert to this revision