.toPrecision()
The .toPrecision()
method converts a number to a string of its specified precision form.
Syntax
num.toPrecision(precision);
precision
(optional): The number of significant digits.
Examples
Converting number to a string:
const pi = 3.14159;console.log(pi.toPrecision(5)); // Output: 3.1415console.log(pi.toPrecision(3)); // Output: 3.14console.log(pi.toPrecision(1)); // Output: 3
To verify if it is a string:
const pi = 3.14159;console.log(typeof pi.toPrecision());// Output: string
Codebyte Example
Before running the program, what do you think the result will be?
All contributors
- garanews222 total contributions
- christian.dinh2481 total contributions
- Anonymous contributorAnonymous contributor3077 total contributions
- garanews
- christian.dinh
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.