toPrecision()
Number
instance method
JS Home <<
JS Reference <<
Number <<
toPrecision()
Description
Returns a string representation of the number in fixed or exponential notation.
Syntax
Signature | Description |
---|---|
aNumber.toPrecision([numOfPrecisionDigits]) | Returns a string representation of the number in fixed or exponential notation. |
Parameters
Parameter | Description |
---|---|
numOfPrecisionDigits | An optional integer specifying the precision of the fractional digits to display.
|
Examples
The code below displays some precision values.
// Create an array for strings and populate.
var aNumber = 16.546546546;
var stringValue = new Array(4);
stringValue[0] = aNumber.toPrecision();
stringValue[1] = aNumber.toPrecision(2); // 2 precision
stringValue[2] = aNumber.toPrecision(4); // 4 precision
stringValue[3] = aNumber.toPrecision(6); // 6 precision
alert(stringValue);
Related Tutorials
JavaScript Advanced Tutorials - Lesson 3 - Number