unc() - JavaScript - MDN - Mozilla
Có thể bạn quan tâm
- Skip to main content
- Skip to search
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
- Learn more
- See full compatibility
- Report feedback
The Math.trunc() static method returns the integer part of a number by removing any fractional digits.
In this article
- Try it
- Syntax
- Description
- Examples
- Specifications
- Browser compatibility
- See also
Try it
console.log(Math.trunc(13.37)); // Expected output: 13 console.log(Math.trunc(42.84)); // Expected output: 42 console.log(Math.trunc(0.123)); // Expected output: 0 console.log(Math.trunc(-0.123)); // Expected output: -0Syntax
jsMath.trunc(x)Parameters
xA number.
Return value
The integer part of x.
Description
The way Math.trunc() works is more straightforward than the other three Math methods: Math.floor(), Math.ceil() and Math.round(); it truncates (cuts off) the dot and the digits to the right of it, no matter whether the argument is a positive or negative number.
Because trunc() is a static method of Math, you always use it as Math.trunc(), rather than as a method of a Math object you created (Math is not a constructor).
Examples
Using Math.trunc()
jsMath.trunc(-Infinity); // -Infinity Math.trunc("-1.123"); // -1 Math.trunc(-0.123); // -0 Math.trunc(-0); // -0 Math.trunc(0); // 0 Math.trunc(0.123); // 0 Math.trunc(13.37); // 13 Math.trunc(42.84); // 42 Math.trunc(Infinity); // InfinityUsing bitwise no-ops to truncate numbers
Warning: This is not a polyfill for Math.trunc() because of non-negligible edge cases.
Bitwise operations convert their operands to 32-bit integers, which people have historically taken advantage of to truncate float-point numbers. Common techniques include:
jsconst original = 3.14; const truncated1 = ~~original; // Double negation const truncated2 = original & -1; // Bitwise AND with -1 const truncated3 = original | 0; // Bitwise OR with 0 const truncated4 = original ^ 0; // Bitwise XOR with 0 const truncated5 = original >> 0; // Bitwise shifting by 0Beware that this is essentially toInt32, which is not the same as Math.trunc. When the value does not satisfy -231 - 1 < value < 231 (-2147483649 < value < 2147483648), the conversion would overflow.
jsconst a = ~~2147483648; // -2147483648 const b = ~~-2147483649; // 2147483647 const c = ~~4294967296; // 0Only use ~~ as a substitution for Math.trunc() when you are confident that the range of input falls within the range of 32-bit integers.
Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification# sec-math.trunc |
Browser compatibility
See also
- Polyfill of Math.trunc in core-js
- Math.abs()
- Math.ceil()
- Math.floor()
- Math.round()
- Math.sign()
Help improve MDN
Was this page helpful to you? Yes No Learn how to contributeThis page was last modified on Jul 10, 2025 by MDN contributors.
View this page on GitHub • Report a problem with this content Filter sidebar- Standard built-in objects
- Math
- Static methods
- abs()
- acos()
- acosh()
- asin()
- asinh()
- atan()
- atan2()
- atanh()
- cbrt()
- ceil()
- clz32()
- cos()
- cosh()
- exp()
- expm1()
- f16round()
- floor()
- fround()
- hypot()
- imul()
- log()
- log1p()
- log2()
- log10()
- max()
- min()
- pow()
- random()
- round()
- sign()
- sin()
- sinh()
- sqrt()
- sumPrecise()
- tan()
- tanh()
- trunc()
- Static properties
- E
- LN2
- LN10
- LOG2E
- LOG10E
- PI
- SQRT1_2
- SQRT2
Từ khóa » Hàm Trunc Trong C++
-
Hàm Toán Học (Math Function) Trong C++ - Freetuts
-
TRUNC (Hàm TRUNC) - Microsoft Support
-
C++ Trunc() - C++ Standard Library - Programiz
-
Các Hàm Tính Toán (Math Function) Trong C++ - Học C++ Căn Bản Và ...
-
Hàm Trunc() Trong Python - Lập Trình Từ Đầu
-
Hướng Dẫn Cách Phân Biệt Hàm TRUNC Với Hàm INT Trong Excel
-
Trunc - C++ Reference
-
Cách Sử Dụng Hàm TRUNC Cắt Bỏ Phần Thập Phân Trong Excel
-
Python unc() Method - W3Schools
-
Sử Dụng File Siêu Cơ Bản Với C++ - CodeLearn
-
Cách Sử Dụng Hàm TRUNC Trong Excel - Máy Tính
-
Cách Sử Dụng Hàm TRUNC Trong Microsoft Excel - Máy Tính