HTML and CSS Reference
In-Depth Information
where value is the value you want to test for being finite. Like the isNaN() function, the
isFinite() function returns a Boolean value: true if the value is a finite number falling
within JavaScript's acceptable range, and false if the numeric value falls outside that
range or if the value is not a number at all.
A program that reports a run-time or logical error may have a mismatched data value;
you can use the isFinite() and isNaN() functions to determine the state of your
data values.
Specifying the Number Format
When JavaScript displays a numeric value, it stores that value to 16 digits of accuracy.
This can result in long numeric strings of digits being displayed by browsers. For exam-
ple, the code
var x = 1/3;
document.write(“x = “ + x);
causes the following text string to be written to the Web page:
x = 0.3333333333333333
In most cases, you don't need to display a calculated value to 16 digits. For example,
with currency values, you usually want to display results only to two decimal places. To
control the number of digits displayed by browsers, you can apply the method
value .toFixed( n )
where value is the value or variable and n is the number of decimal places that should
be displayed in the output. The following examples show the toFixed() method applied
to different numeric values:
var testValue = 2.835;
testValue.toFixed(0) // returns “3”
testValue.toFixed(1) // returns “2.8”
testValue.toFixed(2) // returns “2.84”
Note that the toFixed() method limits the number of decimals displayed by a value and
converts the value into a text string. Also, the toFixed() method rounds the last digit in
an expression rather than truncating it.
Search WWH ::

Custom Search