- Instant help with your JavaScript coding problems

Convert a float number to integer in JavaScript

Question:
How to convert a float number to integer in JavaScript?
Answer:
const num1 = Math.trunc(11.95);
const num2 = Math.trunc(18.05);

console.log(num1 + ' - ' + num2); // 11 - 18
Description:

If you want the integer (whole) part of a float without rounding, you can use the Math.trunc function. The Math.trunc() function returns the integer part of a number by removing any fractional digits.

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.

Share "How to convert a float number to integer in JavaScript?"