
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Number - toPrecision()
Description
This method returns a string representing the number object to the specified precision.
Syntax
Its syntax is as follows −
number.toPrecision( [ precision ] )
Parameter Details
precision − An integer specifying the number of significant digits.
Return Value
Returns a string representing a Number object in fixed-point or exponential notation rounded toprecision significant digits.
Example
Try the following example.
<html> <head> <title>JavaScript toPrecision() Method </title> </head> <body> <script type = "text/javascript"> var num = new Number(7.123456); document.write("num.toPrecision() is " + num.toPrecision()); document.write("<br />"); document.write("num.toPrecision(4) is " + num.toPrecision(4)); document.write("<br />"); document.write("num.toPrecision(2) is " + num.toPrecision(2)); document.write("<br />"); document.write("num.toPrecision(1) is " + num.toPrecision(1)); </script> </body> </html>
Output
num.toPrecision() is 7.123456 num.toPrecision(4) is 7.123 num.toPrecision(2) is 7.1 num.toPrecision(1) is 7
javascript_number_object.htm
Advertisements