18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 2<br />

The toFixed() method returns a string representation of a number with a specified number of decimal<br />

points. <strong>For</strong> example:<br />

var oNumberObject = new Number(99);<br />

alert(oNumberObject.toFixed(2)); //outputs “99.00”<br />

Here, the toFixed() method is given an argument of 2, which indicates how many decimal places<br />

should be displayed. As a result, the method returns the string “99.00”, filling out the empty decimal<br />

places with 0s. This method can be very useful for applications dealing with currency. The toFixed()<br />

method can represent numbers with 0 to 20 decimal places; other values may cause errors.<br />

Another method related to formatting numbers is the toExponential() method, which returns a string<br />

with the number formatted in e-notation. Just as with toFixed(), toExponential() accepts one argument,<br />

which is the number of decimal places to output. <strong>For</strong> example:<br />

var oNumberObject = new Number(99);<br />

alert(oNumberObject.toExponential(1));<br />

//outputs “9.9e+1”<br />

This code outputs “9.9e+1” as the result, which you may remember from the earlier explanation, represents<br />

9.9 x 10 1 . The question is, what if you don’t know the proper format to use for a number: fixed or<br />

exponential? That’s where the toPrecision() method comes in.<br />

The toPrecision() method returns either the fixed or exponential representation of a number, depending<br />

on which makes the most sense. This method takes one argument, which is the total number of digits<br />

to use to represent the number (not including exponents). Example:<br />

var oNumberObject = new Number(99);<br />

alert(oNumberObject.toPrecision(1));<br />

//outputs “1e+2”<br />

In this example, the task is to represent the number 99 with a single digit, which results in “1e+2”, otherwise<br />

known as 100. Yes, toPrecision() rounded the number to get as close as possible to the actual<br />

value. Because you can’t represent 99 with any fewer than 2 digits, this rounding had to occur. If, however,<br />

you want to represent 99 using two digits, well, that’s easy:<br />

var oNumberObject = new Number(99);<br />

alert(oNumberObject.toPrecision(2)); //outputs “99”<br />

Of course the output is “99”, because that is the exact representation of the number. But what if you<br />

specify more than the number of digits needed?<br />

var oNumberObject = new Number(99);<br />

alert(oNumberObject.toPrecision(3)); //outputs “99.0”<br />

In this case, toPrecision(3) is exactly equivalent to toFixed(1), outputting “99.0” as the result.<br />

The toFixed(), toExponential(), and toPrecision() methods round up or down to accurately<br />

represent a number with the correct number of decimal places.<br />

28

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!