- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to deal with floating point number precision in JavaScript?
To handle floating point number precision in JavaScript, use the toPrecision() method. It helps to format a number to a length you can specify.
Example
<!DOCTYPE html> <html> <body> <script> var num = 28.6754; document.write(num.toPrecision(3)); document.write("<br>"+num.toPrecision(2)); document.write("<br>"+num.toPrecision(5)); </script> </body> </html>
Output
28.7 29 28.675
Advertisements