Rounding to zero
In JavaScript, there are many ways to round numbers, that is, remove the decimal part and only keep the integer part. Among them, there are several commonly used methods to round 0 (also known as truncating the decimal part):
-
use
()
:()
The method will remove the decimal part of a number and return its integer part, but will not round.let num = 4.9; let truncated = (num); // 4 (truncated);
-
Use bitwise operators
|
(bit-wise or):
Bitwise operator|
When processing numbers, the operand is converted into a 32-bit integer, thereby truncating the fractional part.let num = 4.9; let truncated = num | 0; // 4 (truncated);
-
Use double tilde
~~
:
Double Wave~~
It is also a bit-wise rounding method, with the effect and|
Similarly, the decimal part will be truncated.To sum up, the most recommended method is to uselet num = 4.9; let truncated = ~~num; // 4 (truncated);
()
, because it has clear semantics, easy to read code, and has good performance. Although other methods can also achieve the effect of rounding to zero, they may be slightly inferior in readability and maintenance. use
()
and()
Combination of (only if the number is positive):
Although()
and()
is a method for rounding downward and upward, but you can choose which one to use by judging the symbol of the number to achieve the effect of rounding to 0. However, this method is relatively complex and not as intuitive as the previous methods.
function truncate(num) { return num < 0 ? (num) : (num); } let num = 4.9; let truncated = truncate(num); // 4 (truncated); let negNum = -4.9; let negTruncated = truncate(negNum); // -4 (negTruncated);
5. Use string operations:
You can convert the number into a string, then remove the decimal point and the part after it, and finally convert it back to the number. Although this method can achieve the effect, it is low efficiency and not intuitive enough, so it is not recommended to use it.
function truncate(num) { return parseInt((), 10); } let num = 4.9; let truncated = truncate(num); // 4 (truncated);
Common rounding functions
In JavaScript, there are many ways to round numbers. These methods have their own characteristics and are suitable for different scenarios. The following are several common rounding functions and their detailed explanations:
-
()
- Function: round down, that is, return the maximum integer less than or equal to the given number.
-
grammar:
(x)
-
Example:
((4.9)); // Output: 4((-4.1)); // Output: -5
-
()
- Function: round upward, that is, return the smallest integer greater than or equal to the given number.
-
grammar:
(x)
-
Example:
((4.1)); // Output: 5((-4.9)); // Output: -4
-
()
- Function: Rounded, that is, return the integer closest to the given number.
-
grammar:
(x)
-
Example:
((4.5)); // Output: 5((4.4)); // Output: 4((-4.5)); // Output: -4
-
()
- Function: Remove the decimal part, only the integer part is retained, and no rounding is performed.
-
grammar:
(x)
-
Example:
((4.9)); // Output: 4((-4.1)); // Output: -4
Bit operator rounding
-
Function: Pass the bit operator
|
、^
、&
、~
、<<
、>>
、>>>
You can convert floating point numbers into integers, which is equivalent to removing the decimal part (similar to, but only valid for 32-bit integers).
-
Example:
(4.9 | 0); // Output: 4(-4.1 | 0); // Output: -4
- Notice: Bit operator rounding is only applicable to the 32-bit integer range (-231-1), and out of range will result in loss of accuracy.
-
Function: Pass the bit operator
-
Double Negation
-
Function: Convert floating point numbers to integers through double negative operations (equivalent to
)。
-
Example:
(~~4.9); // Output: 4(~~-4.1); // Output: -4
- Notice: The double negative operation is invalid for numbers that exceed the JavaScript safe integer range.
-
Function: Convert floating point numbers to integers through double negative operations (equivalent to
-
String conversion
- Function: rounded by converting strings and intercepting integer parts.
-
Example:
(parseInt(4.9)); // Output: 4(parseInt(-4.1)); // Output: -4
- Notice: This method relies on string conversion and may not perform as well as other built-in methods.
-
toFixed() to turn integer
-
Function:pass
toFixed()
Method converts numbers into strings with specified decimal places and then converts them to integers. -
Example:
const num = 4.9; (parseFloat((0))); // Output: 5const num2 = -4.1; (parseFloat((0))); // Output: -4
-
Notice:
toFixed()
The method returns a string and needs to be converted to a number again.
-
Function:pass
Summarize
- (): Round downward.
- (): Round upward.
- ():rounding.
- (): Remove the decimal part.
- bit operator: Remove the decimal part through bit operations (valid in the 32-bit integer range).
- Double Negative: Remove the decimal part (valid in the 32-bit integer range) through double negative operation.
- String conversion: Convert and intercept the integer part through a string.
- toFixed() to turn integer:pass
toFixed()
The method is converted to a string and then converted to an integer.
Each method has its applicable scenarios and limitations, and the choice needs to be weighed according to the specific needs.
This is the end of this article about Javascript rounding functions and several commonly used methods of rounding to zero. For more related JS rounding functions and rounding to zero, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!