Availability
JavaScript 1.5; JScript 5.5, ECMAScript v3
Synopsis
number.toPrecision(precision)Arguments
precision
The number of significant digits to appear in the returned string.
This may be a value between 1 and 21, inclusive. Implementations are
allowed to optionally support larger and smaller values of
precision. If this argument is omitted,
the toString( ) method is used instead to convert
the number to a base-10 value.
Returns
A string representation of number that
contains precision significant digits. If
precision is large enough to include all
the digits of the integer part of number,
the returned string uses fixed-point notation. Otherwise, exponential
notation is used with one digit before the decimal place and
precision -1 digits after the decimal
place. The number is rounded or padded with zeros as necessary.
Throws
RangeError
If digits is too small or too large.
Values between 1 and 21, inclusive, will not cause a RangeError.
Implementations are allowed to support larger and smaller values as
well.
TypeError
If this method is invoked on an object that is not a Number.
Example
var n = 12345.6789;
n.toPrecision(1); // Returns 1e+4
n.toPrecision(3); // Returns 1.23e+4
n.toPrecision(5); // Returns 12346: note rounding
n.toPrecision(10); // Returns 12345.67890: note added zero
See Also
Number.toExponential( ), Number.toFixed( ), Number.toLocaleString( ),
Number.toString( )
•
Table of Contents
•
Index
•
Reviews
•
Examples
•
Reader Reviews
•
Errata
JavaScript: The Definitive Guide, 4th Edition
By
David Flanagan
Publisher
: O'Reilly
Pub Date
: November 2001
ISBN
: 0-596-00048-0
Pages
: 936
Slots
: 1
This fourth edition of the definitive reference to
JavaScript, a scripting language that can be embedded
directly in web pages, covers the latest version of the
language, JavaScript 1.5, as supported by Netscape 6 and
Internet Explorer 6. The book also provides complete
coverage of the W3C DOM standard (Level 1 and Level 2),
while retaining material on the legacy Level 0 DOM for
backward compatibility.