Sophie

Sophie

distrib > Mageia > 7 > i586 > media > core-release > by-pkgid > 016232f1d9a3f7bee85855d35a2bca58 > files > 63

elixir-doc-1.7.2-1.mga7.noarch.rpm

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="generator" content="ExDoc v0.19.1">
    <title>Float – Elixir v1.7.2</title>
    <link rel="stylesheet" href="dist/app-240d7fc7e5.css" />
      <link rel="canonical" href="https://hexdocs.pm/elixir/v1.7/Float.html" />
    <script src="dist/sidebar_items-cdf4e58b19.js"></script>
    
  </head>
  <body data-type="modules">
    <script>try { if(localStorage.getItem('night-mode')) document.body.className += ' night-mode'; } catch (e) { }</script>
    <div class="main">
<button class="sidebar-button sidebar-toggle">
  <span class="icon-menu" aria-hidden="true"></span>
  <span class="sr-only">Toggle Sidebar</span>
</button>
<button class="sidebar-button night-mode-toggle">
  <span class="icon-theme" aria-hidden="true"></span>
  <span class="sr-only">Toggle Theme</span>
</button>
<section class="sidebar">

  <a href="http://elixir-lang.org/docs.html" class="sidebar-projectLink">
    <div class="sidebar-projectDetails">
      <h1 class="sidebar-projectName">
Elixir      </h1>
      <h2 class="sidebar-projectVersion">
        v1.7.2
      </h2>
    </div>
      <img src="assets/logo.png" alt="Elixir" class="sidebar-projectImage">
  </a>

  <form class="sidebar-search" action="search.html">
    <button type="submit" class="search-button">
      <span class="icon-search" aria-hidden="true"></span>
    </button>
    <input name="q" type="text" id="search-list" class="search-input" placeholder="Search" aria-label="Search" autocomplete="off" />
  </form>

  <ul class="sidebar-listNav">
    <li><a id="extras-list" href="#full-list">Pages</a></li>

      <li><a id="modules-list" href="#full-list">Modules</a></li>

      <li><a id="exceptions-list" href="#full-list">Exceptions</a></li>

  </ul>
  <div class="gradient"></div>
  <ul id="full-list" class="sidebar-fullList"></ul>
</section>

<section class="content">
  <div class="content-outer">
    <div id="content" class="content-inner">


      <h1>
        <small class="visible-xs">Elixir v1.7.2</small>
Float        
          <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/float.ex#L3" title="View Source" class="view-source" rel="help">
            <span class="icon-code" aria-hidden="true"></span>
            <span class="sr-only">View Source</span>
          </a>
      </h1>


        <section id="moduledoc">
<p>Functions for working with floating-point numbers.</p>
<h2 id="module-kernel-functions" class="section-heading">
  <a href="#module-kernel-functions" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Kernel functions
</h2>

<p>There are functions related to floating-point numbers on the <a href="Kernel.html"><code class="inline">Kernel</code></a> module
too. Here is a list of them:</p>
<ul>
<li><a href="Kernel.html#round/1"><code class="inline">Kernel.round/1</code></a>: rounds a number to the nearest integer.
</li>
<li><a href="Kernel.html#trunc/1"><code class="inline">Kernel.trunc/1</code></a>: returns the integer part of a number.
</li>
</ul>
<h2 id="module-known-issues" class="section-heading">
  <a href="#module-known-issues" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Known issues
</h2>

<p>There are some very well known problems with floating-point numbers
and arithmetics due to the fact most decimal fractions cannot be
represented by a floating-point binary and most operations are not exact,
but operate on approximations. Those issues are not specific
to Elixir, they are a property of floating point representation itself.</p>
<p>For example, the numbers 0.1 and 0.01 are two of them, what means the result
of squaring 0.1 does not give 0.01 neither the closest representable. Here is
what happens in this case:</p>
<ul>
<li>The closest representable number to 0.1 is 0.1000000014
</li>
<li>The closest representable number to 0.01 is 0.0099999997
</li>
<li>Doing 0.1 * 0.1 should return 0.01, but because 0.1 is actually 0.1000000014,
the result is 0.010000000000000002, and because this is not the closest
representable number to 0.01, you’ll get the wrong result for this operation
</li>
</ul>
<p>There are also other known problems like flooring or rounding numbers. See
<a href="#round/2"><code class="inline">round/2</code></a> and <a href="#floor/2"><code class="inline">floor/2</code></a> for more details about them.</p>
<p>To learn more about floating-point arithmetic visit:</p>
<ul>
<li><a href="http://0.30000000000000004.com/">0.30000000000000004.com</a>
</li>
<li><a href="http://floating-point-gui.de/">What Every Programmer Should Know About Floating-Point Arithmetic</a>
</li>
</ul>
        </section>

        <section id="summary" class="details-list">
          <h1 class="section-heading">
            <a class="hover-link" href="#summary">
              <span class="icon-link" aria-hidden="true"></span>
              <span class="sr-only">Link to this section</span>
            </a>
            Summary
          </h1>
  <div class="summary-types summary">
    <h2>
      <a href="#types">Types</a>
    </h2>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#t:precision_range/0">precision_range()</a>
  </div>
</div>
  </div>
          
  <div class="summary-functions summary">
    <h2>
      <a href="#functions">Functions</a>
    </h2>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#ceil/2">ceil(number, precision \\ 0)</a>
  </div>
    <div class="summary-synopsis"><p>Rounds a float to the smallest integer greater than or equal to <code class="inline">num</code></p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#floor/2">floor(number, precision \\ 0)</a>
  </div>
    <div class="summary-synopsis"><p>Rounds a float to the largest number less than or equal to <code class="inline">num</code></p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#parse/1">parse(binary)</a>
  </div>
    <div class="summary-synopsis"><p>Parses a binary into a float</p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#ratio/1">ratio(float)</a>
  </div>
    <div class="summary-synopsis"><p>Returns a pair of integers whose ratio is exactly equal
to the original float and with a positive denominator</p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#round/2">round(float, precision \\ 0)</a>
  </div>
    <div class="summary-synopsis"><p>Rounds a floating-point value to an arbitrary number of fractional
digits (between 0 and 15)</p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#to_charlist/1">to_charlist(float)</a>
  </div>
    <div class="summary-synopsis"><p>Returns a charlist which corresponds to the text representation
of the given float</p>
</div>
</div>
<div class="summary-row">
  <div class="summary-signature">
    <a href="#to_string/1">to_string(float)</a>
  </div>
    <div class="summary-synopsis"><p>Returns a binary which corresponds to the text representation
of the given float</p>
</div>
</div>
  </div>
          
        </section>

        <section id="types" class="details-list">
          <h1 class="section-heading">
            <a class="hover-link" href="#types">
              <span class="icon-link" aria-hidden="true"></span>
              <span class="sr-only">Link to this section</span>
            </a>
            Types
          </h1>
          <div class="types-list">
<div class="detail" id="t:precision_range/0">
    <div class="detail-header">
    <a href="#t:precision_range/0" class="detail-link" title="Link to this type">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this type</span>
    </a>
    <span class="signature">precision_range()</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/float.ex#L47" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>precision_range() :: 0..15</pre>
      </div>
  </div>
  <section class="docstring">
  </section>
</div>
          </div>
        </section>


        <section id="functions" class="details-list">
          <h1 class="section-heading">
            <a class="hover-link" href="#functions">
              <span class="icon-link" aria-hidden="true"></span>
              <span class="sr-only">Link to this section</span>
            </a>
            Functions
          </h1>
<div class="detail" id="ceil/2">
  
    <span id="ceil/1"></span>
  <div class="detail-header">
    <a href="#ceil/2" class="detail-link" title="Link to this function">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this function</span>
    </a>
    <span class="signature">ceil(number, precision \\ 0)</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/float.ex#L199" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>ceil(<a href="typespecs.html#basic-types">float</a>(), <a href="#t:precision_range/0">precision_range</a>()) :: <a href="typespecs.html#basic-types">float</a>()</pre>
      </div>
  </div>
  <section class="docstring">
<p>Rounds a float to the smallest integer greater than or equal to <code class="inline">num</code>.</p>
<p><a href="#ceil/2"><code class="inline">ceil/2</code></a> also accepts a precision to round a floating-point value down
to an arbitrary number of fractional digits (between 0 and 15).</p>
<p>The operation is performed on the binary floating point, without a
conversion to decimal.</p>
<p>The behaviour of <a href="#ceil/2"><code class="inline">ceil/2</code></a> for floats can be surprising. For example:</p>
<pre><code class="nohighlight makeup elixir"><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">ceil</span><span class="p" data-group-id="8261451397-1">(</span><span class="o">-</span><span class="mf">12.52</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p" data-group-id="8261451397-1">)</span><span class="w">
</span><span class="o">-</span><span class="mf">12.51</span></code></pre>
<p>One may have expected it to ceil to -12.52. This is not a bug.
Most decimal fractions cannot be represented as a binary floating point
and therefore the number above is internally represented as -12.51999999,
which explains the behaviour above.</p>
<p>This function always returns floats. <a href="Kernel.html#trunc/1"><code class="inline">Kernel.trunc/1</code></a> may be used instead to
truncate the result to an integer afterwards.</p>
<h2 id="ceil/2-examples" class="section-heading">
  <a href="#ceil/2-examples" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Examples
</h2>

<pre><code class="nohighlight makeup elixir"><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">ceil</span><span class="p" data-group-id="4431042873-1">(</span><span class="mf">34.25</span><span class="p" data-group-id="4431042873-1">)</span><span class="w">
</span><span class="mf">35.0</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">ceil</span><span class="p" data-group-id="4431042873-2">(</span><span class="o">-</span><span class="mf">56.5</span><span class="p" data-group-id="4431042873-2">)</span><span class="w">
</span><span class="o">-</span><span class="mf">56.0</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">ceil</span><span class="p" data-group-id="4431042873-3">(</span><span class="mf">34.251</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p" data-group-id="4431042873-3">)</span><span class="w">
</span><span class="mf">34.26</span></code></pre>
  </section>
</div>
<div class="detail" id="floor/2">
  
    <span id="floor/1"></span>
  <div class="detail-header">
    <a href="#floor/2" class="detail-link" title="Link to this function">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this function</span>
    </a>
    <span class="signature">floor(number, precision \\ 0)</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/float.ex#L156" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>floor(<a href="typespecs.html#basic-types">float</a>(), <a href="#t:precision_range/0">precision_range</a>()) :: <a href="typespecs.html#basic-types">float</a>()</pre>
      </div>
  </div>
  <section class="docstring">
<p>Rounds a float to the largest number less than or equal to <code class="inline">num</code>.</p>
<p><a href="#floor/2"><code class="inline">floor/2</code></a> also accepts a precision to round a floating-point value down
to an arbitrary number of fractional digits (between 0 and 15).
The operation is performed on the binary floating point, without a
conversion to decimal.</p>
<p>This function always returns a float. <a href="Kernel.html#trunc/1"><code class="inline">Kernel.trunc/1</code></a> may be used instead to
truncate the result to an integer afterwards.</p>
<h2 id="floor/2-known-issues" class="section-heading">
  <a href="#floor/2-known-issues" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Known issues
</h2>

<p>The behaviour of <a href="#floor/2"><code class="inline">floor/2</code></a> for floats can be surprising. For example:</p>
<pre><code class="nohighlight makeup elixir"><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">floor</span><span class="p" data-group-id="9191778978-1">(</span><span class="mf">12.52</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p" data-group-id="9191778978-1">)</span><span class="w">
</span><span class="mf">12.51</span></code></pre>
<p>One may have expected it to floor to 12.52. This is not a bug.
Most decimal fractions cannot be represented as a binary floating point
and therefore the number above is internally represented as 12.51999999,
which explains the behaviour above.</p>
<h2 id="floor/2-examples" class="section-heading">
  <a href="#floor/2-examples" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Examples
</h2>

<pre><code class="nohighlight makeup elixir"><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">floor</span><span class="p" data-group-id="6529914744-1">(</span><span class="mf">34.25</span><span class="p" data-group-id="6529914744-1">)</span><span class="w">
</span><span class="mf">34.0</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">floor</span><span class="p" data-group-id="6529914744-2">(</span><span class="o">-</span><span class="mf">56.5</span><span class="p" data-group-id="6529914744-2">)</span><span class="w">
</span><span class="o">-</span><span class="mf">57.0</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">floor</span><span class="p" data-group-id="6529914744-3">(</span><span class="mf">34.259</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p" data-group-id="6529914744-3">)</span><span class="w">
</span><span class="mf">34.25</span></code></pre>
  </section>
</div>
<div class="detail" id="parse/1">
    <div class="detail-header">
    <a href="#parse/1" class="detail-link" title="Link to this function">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this function</span>
    </a>
    <span class="signature">parse(binary)</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/float.ex#L87" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>parse(<a href="typespecs.html#built-in-types">binary</a>()) :: {<a href="typespecs.html#basic-types">float</a>(), <a href="typespecs.html#built-in-types">binary</a>()} | :error</pre>
      </div>
  </div>
  <section class="docstring">
<p>Parses a binary into a float.</p>
<p>If successful, returns a tuple in the form of <code class="inline">{float, remainder_of_binary}</code>;
when the binary cannot be coerced into a valid float, the atom <code class="inline">:error</code> is
returned.</p>
<p>If the size of float exceeds the maximum size of <code class="inline">1.7976931348623157e+308</code>,
the <a href="ArgumentError.html"><code class="inline">ArgumentError</code></a> exception is raised.</p>
<p>If you want to convert a string-formatted float directly to a float,
<a href="String.html#to_float/1"><code class="inline">String.to_float/1</code></a> can be used instead.</p>
<h2 id="parse/1-examples" class="section-heading">
  <a href="#parse/1-examples" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Examples
</h2>

<pre><code class="nohighlight makeup elixir"><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">parse</span><span class="p" data-group-id="1284763503-1">(</span><span class="s">&quot;34&quot;</span><span class="p" data-group-id="1284763503-1">)</span><span class="w">
</span><span class="p" data-group-id="1284763503-2">{</span><span class="mf">34.0</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;&quot;</span><span class="p" data-group-id="1284763503-2">}</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">parse</span><span class="p" data-group-id="1284763503-3">(</span><span class="s">&quot;34.25&quot;</span><span class="p" data-group-id="1284763503-3">)</span><span class="w">
</span><span class="p" data-group-id="1284763503-4">{</span><span class="mf">34.25</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;&quot;</span><span class="p" data-group-id="1284763503-4">}</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">parse</span><span class="p" data-group-id="1284763503-5">(</span><span class="s">&quot;56.5xyz&quot;</span><span class="p" data-group-id="1284763503-5">)</span><span class="w">
</span><span class="p" data-group-id="1284763503-6">{</span><span class="mf">56.5</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;xyz&quot;</span><span class="p" data-group-id="1284763503-6">}</span><span class="w">

</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">parse</span><span class="p" data-group-id="1284763503-7">(</span><span class="s">&quot;pi&quot;</span><span class="p" data-group-id="1284763503-7">)</span><span class="w">
</span><span class="ss">:error</span></code></pre>
  </section>
</div>
<div class="detail" id="ratio/1">
    <div class="detail-header">
    <a href="#ratio/1" class="detail-link" title="Link to this function">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this function</span>
    </a>
    <span class="signature">ratio(float)</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/float.ex#L392" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
    
      <span class="note">(since 1.4.0)</span>
    
      <div class="specs">
          <pre>ratio(<a href="typespecs.html#basic-types">float</a>()) :: {<a href="typespecs.html#basic-types">pos_integer</a>() | <a href="typespecs.html#basic-types">neg_integer</a>(), <a href="typespecs.html#basic-types">pos_integer</a>()}</pre>
      </div>
  </div>
  <section class="docstring">
<p>Returns a pair of integers whose ratio is exactly equal
to the original float and with a positive denominator.</p>
<h2 id="ratio/1-examples" class="section-heading">
  <a href="#ratio/1-examples" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Examples
</h2>

<pre><code class="nohighlight makeup elixir"><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">ratio</span><span class="p" data-group-id="8052341449-1">(</span><span class="mf">3.14</span><span class="p" data-group-id="8052341449-1">)</span><span class="w">
</span><span class="p" data-group-id="8052341449-2">{</span><span class="mi">7070651414971679</span><span class="p">,</span><span class="w"> </span><span class="mi">2251799813685248</span><span class="p" data-group-id="8052341449-2">}</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">ratio</span><span class="p" data-group-id="8052341449-3">(</span><span class="o">-</span><span class="mf">3.14</span><span class="p" data-group-id="8052341449-3">)</span><span class="w">
</span><span class="p" data-group-id="8052341449-4">{</span><span class="o">-</span><span class="mi">7070651414971679</span><span class="p">,</span><span class="w"> </span><span class="mi">2251799813685248</span><span class="p" data-group-id="8052341449-4">}</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">ratio</span><span class="p" data-group-id="8052341449-5">(</span><span class="mf">1.5</span><span class="p" data-group-id="8052341449-5">)</span><span class="w">
</span><span class="p" data-group-id="8052341449-6">{</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p" data-group-id="8052341449-6">}</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">ratio</span><span class="p" data-group-id="8052341449-7">(</span><span class="o">-</span><span class="mf">1.5</span><span class="p" data-group-id="8052341449-7">)</span><span class="w">
</span><span class="p" data-group-id="8052341449-8">{</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p" data-group-id="8052341449-8">}</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">ratio</span><span class="p" data-group-id="8052341449-9">(</span><span class="mf">16.0</span><span class="p" data-group-id="8052341449-9">)</span><span class="w">
</span><span class="p" data-group-id="8052341449-10">{</span><span class="mi">16</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p" data-group-id="8052341449-10">}</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">ratio</span><span class="p" data-group-id="8052341449-11">(</span><span class="o">-</span><span class="mf">16.0</span><span class="p" data-group-id="8052341449-11">)</span><span class="w">
</span><span class="p" data-group-id="8052341449-12">{</span><span class="o">-</span><span class="mi">16</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p" data-group-id="8052341449-12">}</span></code></pre>
  </section>
</div>
<div class="detail" id="round/2">
  
    <span id="round/1"></span>
  <div class="detail-header">
    <a href="#round/2" class="detail-link" title="Link to this function">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this function</span>
    </a>
    <span class="signature">round(float, precision \\ 0)</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/float.ex#L255" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>round(<a href="typespecs.html#basic-types">float</a>(), <a href="#t:precision_range/0">precision_range</a>()) :: <a href="typespecs.html#basic-types">float</a>()</pre>
      </div>
  </div>
  <section class="docstring">
<p>Rounds a floating-point value to an arbitrary number of fractional
digits (between 0 and 15).</p>
<p>The rounding direction always ties to half up. The operation is
performed on the binary floating point, without a conversion to decimal.</p>
<p>This function only accepts floats and always returns a float. Use
<a href="Kernel.html#round/1"><code class="inline">Kernel.round/1</code></a> if you want a function that accepts both floats
and integers and always returns an integer.</p>
<h2 id="round/2-known-issues" class="section-heading">
  <a href="#round/2-known-issues" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Known issues
</h2>

<p>The behaviour of <a href="#round/2"><code class="inline">round/2</code></a> for floats can be surprising. For example:</p>
<pre><code class="nohighlight makeup elixir"><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">round</span><span class="p" data-group-id="3053212782-1">(</span><span class="mf">5.5675</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p" data-group-id="3053212782-1">)</span><span class="w">
</span><span class="mf">5.567</span></code></pre>
<p>One may have expected it to round to the half up 5.568. This is not a bug.
Most decimal fractions cannot be represented as a binary floating point
and therefore the number above is internally represented as 5.567499999,
which explains the behaviour above. If you want exact rounding for decimals,
you must use a decimal library. The behaviour above is also in accordance
to reference implementations, such as “Correctly Rounded Binary-Decimal and
Decimal-Binary Conversions” by David M. Gay.</p>
<h2 id="round/2-examples" class="section-heading">
  <a href="#round/2-examples" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Examples
</h2>

<pre><code class="nohighlight makeup elixir"><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">round</span><span class="p" data-group-id="9774794383-1">(</span><span class="mf">12.5</span><span class="p" data-group-id="9774794383-1">)</span><span class="w">
</span><span class="mf">13.0</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">round</span><span class="p" data-group-id="9774794383-2">(</span><span class="mf">5.5674</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p" data-group-id="9774794383-2">)</span><span class="w">
</span><span class="mf">5.567</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">round</span><span class="p" data-group-id="9774794383-3">(</span><span class="mf">5.5675</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p" data-group-id="9774794383-3">)</span><span class="w">
</span><span class="mf">5.567</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">round</span><span class="p" data-group-id="9774794383-4">(</span><span class="o">-</span><span class="mf">5.5674</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p" data-group-id="9774794383-4">)</span><span class="w">
</span><span class="o">-</span><span class="mf">5.567</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">round</span><span class="p" data-group-id="9774794383-5">(</span><span class="o">-</span><span class="mf">5.5675</span><span class="p" data-group-id="9774794383-5">)</span><span class="w">
</span><span class="o">-</span><span class="mf">6.0</span><span class="w">
</span><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">round</span><span class="p" data-group-id="9774794383-6">(</span><span class="mf">12.341444444444441</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span><span class="p" data-group-id="9774794383-6">)</span><span class="w">
</span><span class="mf">12.341444444444441</span></code></pre>
  </section>
</div>
<div class="detail" id="to_charlist/1">
    <div class="detail-header">
    <a href="#to_charlist/1" class="detail-link" title="Link to this function">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this function</span>
    </a>
    <span class="signature">to_charlist(float)</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/float.ex#L452" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>to_charlist(<a href="typespecs.html#basic-types">float</a>()) :: <a href="typespecs.html#built-in-types">charlist</a>()</pre>
      </div>
  </div>
  <section class="docstring">
<p>Returns a charlist which corresponds to the text representation
of the given float.</p>
<p>It uses the shortest representation according to algorithm described
in “Printing Floating-Point Numbers Quickly and Accurately” in
Proceedings of the SIGPLAN ‘96 Conference on Programming Language
Design and Implementation.</p>
<h2 id="to_charlist/1-examples" class="section-heading">
  <a href="#to_charlist/1-examples" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Examples
</h2>

<pre><code class="nohighlight makeup elixir"><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">to_charlist</span><span class="p" data-group-id="4329107620-1">(</span><span class="mf">7.0</span><span class="p" data-group-id="4329107620-1">)</span><span class="w">
</span><span class="sc">&#39;7.0&#39;</span></code></pre>
  </section>
</div>
<div class="detail" id="to_string/1">
    <div class="detail-header">
    <a href="#to_string/1" class="detail-link" title="Link to this function">
      <span class="icon-link" aria-hidden="true"></span>
      <span class="sr-only">Link to this function</span>
    </a>
    <span class="signature">to_string(float)</span>
      <a href="https://github.com/elixir-lang/elixir/blob/v1.7.2/lib/elixir/lib/float.ex#L472" class="view-source" rel="help" title="View Source">
       <span class="icon-code" aria-hidden="true"></span>
       <span class="sr-only">View Source</span>
     </a>
        
      <div class="specs">
          <pre>to_string(<a href="typespecs.html#basic-types">float</a>()) :: <a href="String.html#t:t/0">String.t</a>()</pre>
      </div>
  </div>
  <section class="docstring">
<p>Returns a binary which corresponds to the text representation
of the given float.</p>
<p>It uses the shortest representation according to algorithm described
in “Printing Floating-Point Numbers Quickly and Accurately” in
Proceedings of the SIGPLAN ‘96 Conference on Programming Language
Design and Implementation.</p>
<h2 id="to_string/1-examples" class="section-heading">
  <a href="#to_string/1-examples" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
  Examples
</h2>

<pre><code class="nohighlight makeup elixir"><span class="gp unselectable">iex&gt; </span><span class="nc">Float</span><span class="o">.</span><span class="n">to_string</span><span class="p" data-group-id="7661968937-1">(</span><span class="mf">7.0</span><span class="p" data-group-id="7661968937-1">)</span><span class="w">
</span><span class="s">&quot;7.0&quot;</span></code></pre>
  </section>
</div>
        </section>

          <footer class="footer">
        <p>
          <span class="line">
            Built using
            <a href="https://github.com/elixir-lang/ex_doc" title="ExDoc" target="_blank" rel="help noopener">ExDoc</a> (v0.19.1),
          </span>
          <span class="line">
            designed by
            <a href="https://twitter.com/dignifiedquire" target="_blank" rel="noopener" title="@dignifiedquire">Friedel Ziegelmayer</a>.
            </span>
        </p>
      </footer>
    </div>
  </div>
</section>
</div>
  <script src="dist/app-a0c90688fa.js"></script>
  
  </body>
</html>