Number toPrecision

Description

toPrecision() returns either the fixed or the exponential representation of a number, depending on which makes the most sense.

This method takes one argument, which is the total number of digits to use to represent the number not including exponents.

Example


var num = 99;
console.log(num.toPrecision(1));    //"1e+2"
console.log(num.toPrecision(2));    //"99"
console.log(num.toPrecision(3));    //"99.0"
//from ww  w.  ja va 2s .c om

The toPrecision() method can represent numbers with 1 through 21 decimal places.

The code above generates the following result.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions