JavaScript Variable coercion/conversion Float to Integer


Example

To convert a float to an integer, JavaScript provides multiple methods.

The floor function returns the first integer less than or equal to the float.

Math.floor(5.7); // 5

The ceil function returns the first integer greater than or equal to the float.

Math.ceil(5.3); // 6

The round function rounds the float.

Math.round(3.2); // 3
Math.round(3.6); // 4
6

Truncation (trunc) removes the decimals from the float.

Math.trunc(3.7); // 3

Notice the difference between truncation (trunc) and floor:

Math.floor(-3.1); // -4
Math.trunc(-3.1); // -3