spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / java_core / 2 To page 1To page 2To page 3To page 4current pageTo page 6
[previous] [next]

Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


JavaScript by Example: JavaScript Core Objects. Pt. 2

Formatting Numbers

To convert floating-point numbers to a string with a specified number of significant digits, JavaScript provides the toFixed() and toExponential() methods.

 

<html>

<head><title>Number Object</title>

</head>

<body bgcolor=orange><font color="black" size="+1">

<h2>

Formatting Numbers

</h2>

<script language="JavaScript">

1 var n = new Number(22.425456);

2 document.write("<b>The unformatted number is " + n + "<br>");

3 document.write("The formatted number is "+ n.toFixed(2) +

"<br>");

4 document.write("The formatted number is "+ n.toFixed(3) +

"<br>");

</script>

</body>

</html>

 

 
A new Number object is created and assigned to the variable n.

  1. The value of the number is displayed as a large floating-point number, 22.425456.

  2. The Number object's toFixed() method gets an argument of 2. This fixes the decimal point two places to the right of the decimal point and rounds up if necessary. The new value is 22.43.

  3. This time the toFixed() method will format the number with three numbers to the right of the decimal point. (See Figure 9.30.)

Using the toFixed() Number method.

The Boolean Object

The Boolean object was included in JavaScript 1.1. It is used to convert a non-Boolean value to a Boolean value, either true or false. There is one property, the prototype property, and one method, the toString() method, which converts a Boolean value to a string; thus, true is converted to "true" and false is converted to "false".

 

var object = new Boolean(value);

 

Example:

 

var b1 = new Boolean(5);

var b2 = new Boolean(null);

 

 

<html><head><title>Boolean Object</title>

</head>

<body bgcolor=aqua>

<font face="arial" size="+1"><b>

The Boolean Object<br>

<font size="-1">

<script language="JavaScript">

1 var bool1= new Boolean( 0);

var bool2 = new Boolean(1);

var bool3 = new Boolean("");

var bool4 = new Boolean(null);

var bool5 = new Boolean(NaN);

2 document.write("The value 0 is boolean "+ bool1 +"<br>");

document.write("The value 1 is boolean "+ bool2 +"<br>");

document.write("The value of the empty string is boolean "+

bool3+ "<br>");

document.write("The value of null is boolean "+ bool4+ "<br>");

document.write("The value of NaN is boolean "+ bool5 +"<br>");

</script>

</body></html>

 

 
The argument passed to the Boolean object constructor is the initial value of the object, either true or false. If the initial value is 0, the empty string "", NaN, or null, the result is false; otherwise, the result is true.

  1. The Boolean object's values are displayed as either true or false. (See Figure 9.31.)

True or False?

home / programming / java_core / 2 To page 1To page 2To page 3To page 4current pageTo page 6
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

Created: March 27 2003
Revised: November 19, 2003

URL: http://webreference.com/programming/java_core/2