C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Math trunc() methodThe JavaScript math trunc() method removes the fractional part of the given number and returns the remaining integer part only. SyntaxThe trunc() method is represented by the following syntax: Math.trunc(num) Parameternum - A number. ReturnThe integer part of the given number. JavaScript Math trunc() method exampleHere, we will understand trunc() method through various examples. Example 1Let's see an example to print integer part of the given numbers. <script> document.writeln(Math.trunc(12.24)+ "<br>"); document.writeln(Math.trunc(0.84)); </script> Output: 12 0 Example 2In this example, we will use trunc() method with negative numbers. <script> document.writeln(Math.trunc(-12.24)+ "<br>"); document.writeln(Math.trunc(-0.84)); </script> Output: -12 0 Example 3Here, you can test trunc() method with your own test cases. <script> function display() { var x=document.getElementById("num").value; document.getElementById("result").innerHTML=Math.trunc(x); } </script> <form> Enter a number: <input type="text" id="num"> <input type="button" onclick="display()" value="submit"> </form> <p><span id="result"></span></p>
Next TopicJavaScript Math
|