Normally a "number" is just a variable that you assigned a numerical value to. But JavaScript provides a Number object that gives you some powerful formatting routines for working with numbers.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site
   WARNING!
What you learn from these Totally FREE
Einstein Newsletters could cause your friends to mistake you for someone else!
  [] automobiles
  [] business
  [] parenting
  [] computers
  [] contests
  [] education
  [] entertainment
  [] food/wine
  [] free stuff
  [] genealogy
  [] health/fitness
  [] home/garden
  [] humor
  [] marketing
  [] investing
  [] pets
  [] inspiration
  [] self-improve
  [] recreation
  [] travel
  [] womens stuff
  [] writing/reading
Click here and choose as many as you like!

Java Script Number Object

Normally a "number" is just a variable that you assigned a numerical value to. But JavaScript provides a Number object that gives you some powerful formatting routines for working with numbers.

Number Object Methods

MethodReturns
toExponential(digits) A string containing the number in exponential form with digits specifying how many digits to the right of the decimal point should be returned.
toFixed(digits) A string containing the number in fixed point form with digits specifying how many digits to the right of the decimal point should be returned.
toLocaleString() A string that conforms to the client browser
toPrecision(digits) A string in exponential or fixed point form with digits specifying how many digits should be returned.
toString(radix) Returns the number as a string. The default radix parameter (10) converts the value to base-10 notation, or you can specify 2 for binary or 16 for hexadecimal to convert it to that base.
valueOf() Returns the value of the number contained by the Number object

To demonstrate the use of the Number object along with some Math object methods, lets calculate a very large number, for example the volume of the earth in cubic miles. The equation for the volume of a sphere is shown below.

volume = 4 pi r ^ 3 / 3

Note: Our volume of the earth result using this equation will not be perfectly accurate because the earth is a not a perfect sphere, its flattened at the poles. For our calculation we will use the radius of the earth at the equator, 3963.19 miles.

First we'll use the Math.pow method to cube or raise the power of the radius to the third power as shown below.

Math.pow(3963.19,3)

We plug that into a statement that calculates the numerator for the right side of our equation, as shown below.

4 * Math.PI * (Math.pow(3963.19,3)

Note that we are using the Math.PI property to provide us with the value of pi.

Next we divide the numerator by the denominator, 3, as shown below.

4 * Math.PI * (Math.pow(3963.19,3))/3

We create a Number object and initialize it with the result of this calculation, as shown below.

var volume = new Number(4*Math.PI*(Math.pow(3963.19,3))/3);

If we write the result of this calculation as it stands, the value is incomprehensible, as shown below.

260749383073.77307

Web Design Sections
Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269