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 Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


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, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27 2003
Revised: November 19, 2003

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