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.

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
Copyright bretteast ©


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

  <meta charset="UTF-8">
  <link rel='stylesheet prefetch' href=''>

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



  <div class="container">

<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=""></a>.</p>

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


<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>


<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>
  <script src=''></script>


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



/*Downloaded from */
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 */
var num = new Number(9234.4354);

var num2 = num.toExponential();

var num3 = num.toExponential(1);

var num4 = num.toExponential(2);

var num5 = num.toExponential(10);

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

var num7 = num.toExponential(0);

var number = 852.46;

var number9 = number.toExponential();

var number10 = number.toExponential(1);

var number11 = number.toExponential(2);

$('#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();