In Javascript toPrecision ( ) method is used to represent a number either in exponential form or in fixed-point length with a specified number of digits .
Syntax ->
number.toPrecision ( [ precision lenght ] )
Parameters ->
number -> It is mandatory and required a Number Object .
precision -> It is optional and take the specified length of an number and length should be in range of 1 - 21(inclusive) .
Return > For exponential form , precision-1 digits are returned after the decimal point and if there is a fixed length then precision signified digits are returned . And if precision argument is not supplied then it would undefined .
Example ->
var num = new Number ( 100 ) ;
var prec = num.toPrecision ( ) ;
document.write ( prec ) ; //Output -> 100
var num = new Number ( 100.123787 ) ;
var prec = num.toPrecision ( 5 ) ;
document.write ( fix ) ; //Output -> 100.12
Error Throws ->
RangeError -> The value of precision should be in between 1 to 100 ( inclusive ) ,otherwise RangeError will be throw . And it allowed to support larger and smaller value also.
0 Comment(s)