JavaScript & jQuery Tutorials

JS Home Right Arrow JS Reference Right Arrow Number Right Arrow toPrecision()

toPrecision()  Number  instance method

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



  

go to home page Homepage go to top of page Top