
- 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
Rounding and truncating numbers in JavaScript.
There are two functions in JavaScript for rounding and truncating numbers: Math.round() and Math.trunc() respectively −
- Math.round() − = It rounds the decimal number to the nearest integer value.
- Math.trunc() − = It simply removes the fractional part of the decimal number and converts it to a whole number.
Following is the code for rounding and truncating numbers in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result, .sample { font-size: 20px; font-weight: 500; color: blueviolet; } .sample { color: red; } </style> </head> <body> <h1>Rounding and truncating numbers in JavaScript</h1> <div class="sample">3.999</div> <div class="result"></div> <br /> <button class="Btn">Click Here</button> <h3>Click on the above button to round and truncate the above number</h3> <script> let resEle = document.querySelector(".result"); let BtnEle = document.querySelector(".Btn"); BtnEle.addEventListener("click", () => { resEle.innerHTML = Math.round(3.9999) + "<br>"; resEle.innerHTML += Math.trunc(3.9999) + "<br>"; }); </script> </body> </html>
Output
On clicking the ‘Click Here’ button −
- Related Articles
- Rounding off numbers to some nearest power in JavaScript
- Truncating a String in JavaScript
- Dividing a floating number, rounding it to 2 decimals, and calculating the remainder in JavaScript
- Using Rounding formula in SAP
- Truncating multiple strings after 100 characters in ABAP
- Generate random characters and numbers in JavaScript?
- Reversing negative and positive numbers in JavaScript
- Numbers and operands to words in JavaScript
- Difference between numbers and string numbers present in an array in JavaScript
- Minimize Rounding Error to Meet Target in C++
- What is mean by rounding off?
- Rounding off to nearest thousands :87990
- How to avoid rounding errors while calculating in Excel?
- How to verify action bar tittle is truncating or not in android?
- Pronic numbers in JavaScript

Advertisements