Number Methods

EasyProgramming.net tutorial on number methods!

by Nazmus Nasir

HTML

<!-- Easy JavaScript #12 - Number Methods --><!-- Easy JavaScript #11 - string methods -->

<p>
Welcome to the 12th Easy JavaScript tutorial! Let's go over the number object. 
</p>

<h3>
My Number: <span id="myNumber"></span>
</h3>
<div class="display">
num.toString() is <span id="toString"></span>
</div>
<div class="display">
num.toExponential() is <span id="toExponential"></span>
</div>
<div class="display">
num.toFixed() to 3 decimal places is <span id="toFixed"></span>
</div>
<div class="display">
num.toPrecision() to 3 digits is <span id="toPrecision"></span>
</div>
<div class="display">
My number's character length is <span id="length"></span>
</div>

<h3 id="buffer">
Helpful Info
</h3>
<h4>
Properties of the "Number" object:
</h4>
<ul>
<li><strong>MAX_VALUE</strong> - returns the largest possible number - Number.MAX_VALUE</li>
<li><strong>MIN_VALUE</strong> - returns the largest possible number - Number.MIN_VALUE</li>
</ul>
<h4>
Methods:
</h4>
<ul>
<li><strong>toString()</strong> - Converts number to String - same as String()</li>
<li><strong>toExponential()</strong> - Converts number to exponential notation</li>
<li><strong>toFixed()</strong> - Returns a string with a specified number of decimals</li>
<li><strong>toPrecision()</strong> - Returns a string with a specified number of digits</li>
<li><strong>parseInt()</strong> - Attempts to convert data to integer</li>
<li><strong>parseFloat()</strong> - Attempts to convert data to floating point</li>

</ul>

<p>
More in-depth information at <a href="http://www.w3schools.com/js/js_number_methods.asp">http://www.w3schools.com/js/js_number_methods.asp</a>
</p>
<p>
Visit <a href="http://www.easyprogramming.net">Easy Programming</a> for more information. 
</p>

CSS

#buffer{
  padding-top: 50px;
}
.display{
  line-height: 20px;
}

.display > span{
  font-weight: bold;
  color: #ff6600;
}

JavaScript

//numbers

var num = 28.2749338823;

console.log("JS Min Number Value is: " + Number.MIN_VALUE);
console.log("JS Max Number Value is: " + Number.MAX_VALUE);

//syntax is variable.toString()
document.getElementById("toString").innerText = num.toString();

//num.toExponential("number of digits to post/round to"); - paramter optional
document.getElementById("toExponential").innerText = num.toExponential(3);

//toFixed("number of decimal places") - can include 0
document.getElementById("toFixed").innerText = num.toFixed(3);

//toPrecition("number of digits to display")
document.getElementById("toPrecision").innerText = num.toPrecision(3);

//convert number to a string, then get the length! 
document.getElementById("length").innerText = num.toString().length;