Math.trunc()
The Math.trunc() static method returns the integer part of a number by removing any fractional digits.
Try it
Syntax
Parameters
Return value
The integer part of x .
Description
Unlike the other three Math methods: Math.floor() , Math.ceil() and Math.round() , the way Math.trunc() works is very simple. 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()
Using 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:
Как отбросить дробную часть в javascript
The Javascript Math.trunc() method is used to return the integer part of a floating-point number by removing the fractional digits. In other words, Math.trunc() function cuts off the dot and the digits to the right of it.
Syntax:
Parameters: This method accepts a single parameter as mentioned above and described below:
- value. It is the floating-point number that is to be sent to the Math.trunc() function.
Return Value: The Math.trunc() method returns the integer part of the given number. More codes for the above method are as follows:
Below is an example of the Math trunc() Method.
Example: When a positive float number passed as a parameter
Math.trunc()
The Math.trunc() function returns the integer part of a number by removing any fractional digits.
Syntax
Parameters
Return value
The integer part of the given number.
Description
Unlike the other three Math methods: Math.floor() , Math.ceil() and Math.round() , the way Math.trunc() works is very simple. 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.
The argument passed to this method will be converted to number type implicitly.
How can I remove the decimal part from JavaScript number?
I have the results of a division and I wish to discard the decimal portion of the resultant number.
How can I do this?
14 Answers 14
-
(truncate fractional part, also see below) (round down) (round up) (round to nearest integer)
. dependent on how you wanted to remove the decimal.
Math.trunc() isn’t supported on all platforms yet (namely IE), but you could easily use a polyfill in the meantime.
Another method of truncating the fractional portion with excellent platform support is by using a bitwise operator (.e.g |0 ). The side-effect of using a bitwise operator on a number is it will treat its operand as a signed 32bit integer, therefore removing the fractional component. Keep in mind this will also mangle numbers larger than 32 bits.
You may also be talking about the inaccuracy of decimal rounding with floating point arithmetic.