mozilla

Revision 482435 of Math.fround()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Math/fround
  • Revision title: Math.fround()
  • Revision id: 482435
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment This is actually doc bug 900125

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

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 Float32Array are supported:

function fround(x) {
  var f32 = new Float32Array(1);
  return f32[0] = x, f32[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 (WebKit)
Basic support {{CompatNo}} {{CompatGeckoDesktop("26")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatGeckoMobile("26")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

See also

  • The {{jsxref("Global_Objects/Math", "Math")}} object it belongs to.

Revision Source

<div>
  {{JSRef("Global_Objects", "Math")}} {{harmony}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>Math.fround()</strong></code> function returns the nearest <a class="external" href="http://en.wikipedia.org/wiki/Single_precision" title="link to the wikipedia page on single precision">single precision</a> float representation of a number.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>Math.fround(<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>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>
<pre class="brush: js  language-js" data-number="">
<code class="language-js">Math<span class="token punctuation">.</span><span class="token function">fround<span class="token punctuation">(</span></span><span class="token number">0</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">     // 0
</span>Math<span class="token punctuation">.</span><span class="token function">fround<span class="token punctuation">(</span></span><span class="token number">1</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">     // 1
</span>Math<span class="token punctuation">.</span><span class="token function">fround<span class="token punctuation">(</span></span><span class="token number">1.337</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true"> // 1.3370000123977661
</span>Math<span class="token punctuation">.</span><span class="token function">fround<span class="token punctuation">(</span></span><span class="token number">1.5</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">   // 1.5
</span>Math<span class="token punctuation">.</span><span class="token function">fround<span class="token punctuation">(</span></span><span class="token number">NaN</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">   // NaN</span></code></pre>
<h2 id="Polyfill" name="Polyfill">Polyfill</h2>
<p>This can be emulated with the following function, if Float32Array are supported:</p>
<pre class="brush: js  language-js" data-number="">
<code class="language-js"><span class="token keyword">function</span> <span class="token function">fround<span class="token punctuation">(</span></span>x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> f32 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Float32Array</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> f32<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> x<span class="token punctuation">,</span> f32<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="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">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 (WebKit)</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatGeckoDesktop("26")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatGeckoMobile("26")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li>The {{jsxref("Global_Objects/Math", "Math")}} object it belongs to.</li>
</ul>
Revert to this revision