TypeScript | toPrecision() Function

The toPrecision() method in TypeScript is used to return the string representation in exponential or fixed-point to the specified precision.

Syntax:

number.toPrecision( [ precision ] )

Parameter: It represents an integer value specifying the number of significant digits.

Return Value: The toPrecision() method in TypeScript returns a string representing a Number in fixed-point or exponential notation round to precision significant digits.

Below examples illustrate the working of toPrecision() function in TypeScript:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// toPrecision() method
var num = new Number(6.218956); 
console.log(num.toPrecision()); 
console.log(num.toPrecision(4)); 
console.log(num.toPrecision(3));
</script>

chevron_right


Output:

6.218956 
6.2189 
6.21

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// toPrecision() method
let myNumber: number = 32.5779; 
console.log("Number Method: toPrecision()");  
console.log(myNumber.toPrecision(1));   
console.log(myNumber.toPrecision(3));  
</script>

chevron_right


Output:

Number Method: toPrecision()
3e+1
32.6

full-stack-img

My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.