
- 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
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
- Related Articles
- Reinterpret 64-bit signed integer to a double-precision floating point number in C#
- What is the precision of floating point in C++?
- Convert the specified double-precision floating point number to a 64-bit signed integer in C#
- How to convert a string to a floating point number in JavaScript?
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- Format floating point number in Java
- Fixed Point and Floating Point Number Representations
- How to count set bits in a floating point number in C?
- Convert a floating point number to string in C
- Format floating point with Java MessageFormat
- How to Multiply Two Floating-Point Numbers in Golang?
- Is 'floating-point arithmetic' 100% accurate in JavaScript?
- How to deal with Garbage?
- Haskell Program to convert the string into a floating-point number
- How to increase precision with division in MySQL?

Advertisements