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
numOfPrecisionDigitsAn optional integer specifying the precision of the fractional digits to display.
  • When omitted is the same as Number.toString().
  • A range in the value 0-100 which will throw a RangeError exception if not in this range after rounding.

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);

Press the button below to action the above code:

Related Tutorials

JavaScript Advanced Tutorials - Lesson 3 - Number

JavaScript Basics

JavaScript Basics

JavaScript Intermediate

JavaScript Intermediate

JavaScript Advanced

JavaScript Advanced

JavaScript Reference

JavaScript Entities

Globals

Number

MIN_VALUE
NaN
NEGATIVE_INFINITY
POSITIVE_INFINITY
prototype
Instance Properties
constructor
Class Methods
None.
Instance Methods Getters
toExponential()
toFixed()
toLocaleString()
toPrecision()
toString()
valueOf()
Instance Methods Setters
None.

Statements

Operators