JavaScript Number.toPrecision() Method
You are Here:
JavaScript Number.toPrecision() Method
The Number.toPrecision()
method returns a string representing a Number object in fixed-point or exponential notation rounded to precision significant digits.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var numObj = 12.98765;
var result = numObj.toPrecision(4);
document.write(result);
</script>
</body>
</html>
Syntax
Number.toPrecision(precision)
Parameter Values
Value | Type | Explanation |
---|---|---|
precision | Optional | An integer specifying the number of significant digits. If omitted, it returns the entire number. |
Return Value
Value | Explanation |
---|---|
string | Returns a string representing a Number object in fixed-point or exponential notation rounded to precision significant digits. If omitted, it returns the entire number as string (without any formatting) If the precision argument is a non-integer value, it is rounded to the nearest integer. |
More Examples
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var numObj = 12.98765;
var result1 = numObj.toPrecision(4);
var result2 = numObj.toPrecision();
document.write(result1 +"<br>");
document.write(result2);
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.