Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can I remove the decimal part of JavaScript number?
In this tutorial, we will learn to remove the decimal part of the number in JavaScript. While working with JavaScript, many times, developers need to play with the numbers stored in the database and needs to convert float and double numbers into the pure decimal or integer number.
In this situation, developers can have multiple methods to truncate the decimal part of the number. We will see different approaches to overcome the above problem.
Using the Math.trunc() method
Using the Bitwise operators
Using the Math.ceil() and Math.floor() method
Using the Math.trunc() Method
The Math.trunc() is the most popular method to remove the decimal part of JavaScript. It takes positive, negative, or float numbers as an input and returns the integer part after removing the decimal part. For example, when we give an input of 5.45, it returns 5 only, and for the input of -5.45, it returns -5 only.
Syntax
let decimal = Math.trunc(number)
Parameters
Number ? This parameter represents the number input from which we need to remove the decimal part.
Example
The below example demonstrates the use of the Math.trunc() method in JavaScript.
<!DOCTYPE html>
<html>
<body>
<h2>Removing the decimal part from the number.</h2>
<p>Result after removing the decimal part from 4.96 using Math.trunc() method.</p>
<p id="result"></p>
<script>
let number = 4.96;
let decimal = Math.trunc(number);
document.getElementById("result").innerHTML = decimal;
</script>
</body>
</html>
4
Using Bitwise Operators
In this approach, we will use the different Bitwise operators to cut out the decimal part of the number. To overcome our problem, we can use four different Bitwise operators such as Bitwise OR, Double Not, Right Shift, and Left Shift operators. We will perform the Bitwise OR operation of the input number with 0. Also, in the same way, we can perform the right and left shifts by 0 with the input number, which removes the decimal part from it.
The built-in Math library function takes more calculation time than the Bitwise operation. So, this is the fastest approach to truncate the number.
Syntax
let result = ~~number // Double Not operator let result = number | 0 // Bitwise OR operation let result = number >> 0 // Right shift by 0 let result = number << 0 // Left shift by 0
Example
Users can learn to perform the Bitwise operation with the number to remove the decimal part with the below example. We have used all different Bitwise operators in the below example and print the output.
<!DOCTYPE html>
<html>
<body>
<h2>Removing the decimal part from the number</h2>
<h4>After removing the decimal part from <i>-5.69</i> using Double Not operator.</h4>
<p id="DoubleNot"></p>
<h4>After removing the decimal part from <i>2.34</i> using BitWise OR operator.</h4>
<p id="BitwiseOR"></p>
<h4>After removing the decimal part from <i>100.87</i> using Right Shift operator.</h4>
<p id="RightShift"></p>
<h4>After removing the decimal part from <i>-0.7</i> using Left Shift operator.</h4>
<p id="LeftShift"></p>
<script>
// remove decimal part using the double not operator
let DoubleNot = document.getElementById("DoubleNot");
let number = -5.69;
DoubleNot.innerHTML = ~~number;
// remove decimal part using the Bitwise OR operator
let BitwiseOR = document.getElementById("BitwiseOR");
number = 2.34;
BitwiseOR.innerHTML = number | 0;
// remove decimal part using the Right shift operator
let RightShift = document.getElementById("RightShift");
number = 100.87;
RightShift.innerHTML = number >> 0;
// remove decimal part using the left shift operator
let LeftShift = document.getElementById("LeftShift");
number = -0.7;
LeftShift.innerHTML = number << 0;
</script>
</body>
</html>
-5 2 100 0
Using Math.ceil() and Math.floor() Methods
We will use the Math.ceil() and Math.floor() methods in this approach. The Math.ceil() method returns the smallest integer greater than or equal to the input number. For example, for 5.1, it returns 6.
The Math.floor() method returns the largest integer less than or equal to the input number. For example, for input 5.9, it returns 5.
Syntax
Math.ceil(number) Math.floor(number)
Example
In the below example, we have used the Math.ceil() method and Math.floor() method. Also, we are printing the output of both methods.
<!DOCTYPE html>
<html>
<body>
<h2>Removing the decimal part from the number</h2>
<h4>After removing the decimal part from <i>0.01</i> using the Math.ceil() method.</h4>
<p id="CeilMethod"></p>
<h4>After removing the decimal part from <i>6.99</i> using the Math.floor() method.</h4>
<p id="FloorMethod"></p>
<script>
// remove decimal part using the Math.ceil() method
let CeilMethod = document.getElementById("CeilMethod");
let number = 0.01;
CeilMethod.innerHTML = Math.ceil(number);
// remove decimal part using the Math.floor() method
let FloorMethod = document.getElementById("FloorMethod");
number = 6.99;
FloorMethod.innerHTML = Math.floor(number);
</script>
</body>
</html>
1 6
Comparison
| Method | Performance | Handles Negative Numbers | Best For |
|---|---|---|---|
Math.trunc() |
Medium | Yes | Most cases (readable) |
| Bitwise operators | Fastest | Yes | Performance-critical code |
Math.floor() |
Medium | Yes (rounds down) | Always round down |
Math.ceil() |
Medium | Yes (rounds up) | Always round up |
Conclusion
We have explored three approaches to remove the decimal part of numbers in JavaScript. Math.trunc() is the most readable and recommended for general use, while bitwise operators offer the best performance for high-frequency operations.
