
- 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
What is the importance of Math.trunc() method in JavaScript?
Math.trunc()
Unlike Math.floor(), Math.ceil() and Math.round(), Math.trunc() method removes fractional part and gives out only integer part. It doesn't care about rounding the number to the nearest integer. It doesn't even notice whether the value is positive or negative. Its function is only to chop up the fractional part.
syntax
Math.trunc(x);
parameters - Math.trunc() takes a number as an argument and truncates the fractional part.
In the following example, Math.trunc() method truncated the fractional values of the provided positive numbers.
Example
<html> <body> <script> var val1 = Math.trunc("1.414"); var val2 = Math.trunc("0.49"); var val3 = Math.trunc("8.9"); document.write(val1); document.write("</br>"); document.write(val2); document.write("</br>"); document.write(val3); </script> </body> </html>
Output
1 0 8
In the following Math.trunc() function truncated the fractional values of negative numbers and displayed their integer values in the output.
Example
<html> <body> <script> var val1 = Math.trunc("-1.414"); var val2 = Math.trunc("-0.49"); var val3 = Math.trunc("-8.9"); document.write(val1); document.write("</br>"); document.write(val2); document.write("</br>"); document.write(val3); </script> </body> </html>
Output
-1 0 -8
- Related Articles
- What is the importance of _isEqual() method in JavaScript?
- What is the importance of _without() method in JavaScript?
- What is the importance of _.union() method in JavaScript?
- What is the importance of str.padStart() method in JavaScript?
- What is importance of startsWith() method in JavaScript?
- What is math object in JavaScript?
- What is Math Object in JavaScript Program?
- What is the importance of Symbol.isConcatSpreadable in JavaScript?
- What is the importance of _.initial() function in JavaScript?
- What is the importance of Navigator Object in JavaScript?
- Write the importance of shift() method in javascript array?
- What are JavaScript Math Functions?
- Explain the importance of Array.filter() method in JavaScript with example?
- What is the importance of '/g' flag in JavaScript?
- What is the importance of '/i' flag in JavaScript?

Advertisements