toExponential()

In this example below you will see how to do a toExponential() with some HTML / CSS and Javascript

A pen playing around with the built in prototypal function toExponential() in javascript.

Thumbnail
This awesome code was written by bretteast, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright bretteast ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>toExponential()</title>
  
  
  <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'>

      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div class="container">
<h1>toExponential()</h1>
<h2>Overview</h2>

<p>This method is part of the Number Object prototype and can be called on any object that is a Number, it converts a number to be displayed as an exponent. An exponent is displayed with e+# with # representing the number of places to move a decimal point. Read more about exponential numbers here: <a href="https://en.wikipedia.org/wiki/Scientific_notation">https://en.wikipedia.org/wiki/Scientific_notation</a>.</p>

<h2>Syntax</h2>
<code>number.toExponential( [fractionDigits] );</code>

<h2>Parameters</h2>

<p>The <code>toExponential()</code> method takes one optional parameter called 'fractionDigits'. 'fractionDigits' is a whole number, specifying the number of digits after the decimal point to display before displaying the exponential.</p>

<p>If 'fractionDigits' is not specified, then the number of digits will be the number need to represent the value uniquely.</p>

<h2>Examples</h2>

<p>These examples are taken from the Mozilla Developer Network.</p>

<pre class="ng-scope">var numObj = 77.1234;

console.log(numObj.toExponential());  // logs 7.71234e+1
console.log(numObj.toExponential(4)); // logs 7.7123e+1
console.log(numObj.toExponential(2)); // logs 7.71e+1
console.log(77.1234.toExponential()); // logs 7.71234e+1
console.log(77.toExponential());      // logs 7.7e+1</pre>

<h2>Using a number object</h2>
<p>The result of <code>var num = new Number(9234.4354);</code> is <code class="output1"></code></p>

<p>The result of <code>num.toExponential();</code> is <code class="output2"></code></p>

<p>The result of <code>num.toExponential(1);</code> is <code class="output3"></code></p>

<p>The result of <code>num.toExponential(2);</code> is <code class="output4"></code></p>

<p>The result of <code>num.toExponential(10);</code> is <code class="output5"></code></p>

<p>The result of <code>num.toExponential(-2);</code> is <code class="output6">Uncaught RangeError: toExponential() argument must be between 0 and 20</code></p>

<p>The result of <code>num.toExponential(0);</code> is <code class="output7"></code></p>

<h2>Using just a variable set as a number</h2>
<p>The result of <code>var number = 852.46;</code> is <code class="output8"></code></p>

<p>The result of <code>number.toExponential();</code> is <code class="output9"></code></p>

<p>The result of <code>number.toExponential(1);</code> is <code class="output10"></code></p>

<p>The result of <code>number.toExponential(2);</code> is <code class="output11"></code></p>
  
  <h2>Try for yourself</h2>
  <input id="tryNum" type="text"><code>.toExponential(</code><input id="tryArg" type="text"><code>)</code> returns <code id="tryOutput"></code>
  
</div>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/bretteast/toexponential-MKgjvG */
body {
  font-family: sans-serif;
  background-color: #1d1f20;
  color: #fff;
  font-size: 1.6em;
  margin-left: 24px;
}

p {
  margin-bottom: 12px;
  color: #fff;
}

input {
  color: #000;
}


/*Downloaded from https://www.codeseek.co/bretteast/toexponential-MKgjvG */
var num = new Number(9234.4354);
$('.output1').html(num);

var num2 = num.toExponential();
$('.output2').html(num2);

var num3 = num.toExponential(1);
$('.output3').html(num3);

var num4 = num.toExponential(2);
$('.output4').html(num4);

var num5 = num.toExponential(10);
$('.output5').html(num5);

//var num6 = num.toExponential(-2);
// $('.output6').html(num6);

var num7 = num.toExponential(0);
$('.output7').html(num7);

var number = 852.46;
$('.output8').html(number);

var number9 = number.toExponential();
$('.output9').html(number9);

var number10 = number.toExponential(1);
$('.output10').html(number10);

var number11 = number.toExponential(2);
$('.output11').html(number11);

$('#tryNum, #tryArg').on('keyup change', function () {
    var tryNum = parseFloat($('#tryNum').val(), 10);
    var tryArg = parseInt($('#tryArg').val(), 10);
    var result;
    if (tryArg) {
        try {
            result = tryNum.toExponential(tryArg);
        } catch (e) {
            result = 'Error: ' + e.message;
        }
    } else {
        try {
            result = tryNum.toExponential();
        } catch (e) {
            result = 'Error: ' + e.message;
        }
    }
    //var result = tryArg ? tryNum.toExponential(tryArg) : tryNum.toExponential();
    $('#tryOutput').html(result);
});

Comments