Use trunc() method in JavaScript to get truncate number. Where this method returns the integer part of a number. It’ simply remove the decimals.
Note: This method will NOT round the number up/down to the nearest integer.
Syntax
Math.trunc(num)
Example of JavaScript truncate number | trunc( ) Method
Let’s cuts off the dot and the digits to the right of it in JS.
<!DOCTYPE html>
<html>
<body>
<script language="JavaScript">
var num = 10.85;
var n = Math.trunc(num)
alert(n);
</script>
</body>
</html>
Output:
Q: How to truncate float in JavaScript?
Answer: Use toFixed()
function.
(6.688689).toFixed(); // equal to 7
(6.688689).toFixed(1); // equal to 6.7
(6.688689).toFixed(2); // equal to 6.69
Do comment if you have any doubts and suggestion on this tutorial.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version