How To Use Number Object toPrecision In JavaScript
In this article I am going to explain about Number Object toPrecision Method In JavaScript.
Use toPrecision method in JavaScript
The toPrecision method is used to format a number to a specified length in JavaScript.
This method to add a decimal point for the specified length.
All browsers support number object toPrecision method.
Syntax
Example
<!DOCTYPE html>
<html>
<body style ="background-color:Red">
<p id="demo">Display the formatted number.</p>
<button onclick="myFunction()">Click here</button>
<script type="text/javascript">
function myFunction()
{
var num = new Number(13.3714);
var n = num.toPrecision(2);
var x = document.getElementById("demo");
x.innerHTML = n
}
</script>
</body>
</html>
|
Output1
Output2
Further Readings
You may also want to read these related articles :here
Ask Your Question
Got a programming related question? You may want to post your question here
Programming Answers here