HOWTO · JavaScript

How to Convert a Float Number to Int in JavaScript

Learn how to truncate or round a JavaScript Number, parse decimal text, and avoid unsafe bitwise conversion shortcuts.

On this page

JavaScript does not have separate int and float types. Both are normally represented by the Number type. To remove the fractional part of an existing numeric value, use Math.trunc(), which truncates toward zero. This operation is called truncation toward zero:

console.log(Math.trunc(12.75));
console.log(Math.trunc(-12.75));

Output:

12
-12

The right method depends on the result you need:

  • Use Math.trunc(value) to discard the fractional part toward zero.
  • Use Math.floor(value) or Math.ceil(value) when the direction of rounding matters.
  • Use Math.round(value) to choose the nearest integer.
  • Use parseInt(text, 10) when the input is decimal text, not as the default way to truncate a Number.

Truncate a Number with Math.trunc()

Math.trunc() returns the integer part of a number without rounding. For positive values, this removes everything after the decimal point. For negative values, it moves toward zero, so -12.75 becomes -12, not -13.

const value = -12.75;

console.log(Math.trunc(12.75));
console.log(Math.trunc(value));

Output:

12
-12

This is usually the clearest choice when the value is already numeric and the requirement is “keep the whole-number portion.” A value between -1 and 0 can produce negative zero:

console.log(Object.is(Math.trunc(-0.5), -0));

Output:

true

If your application must distinguish -0 from 0, handle that case explicitly.

Choose floor(), ceil(), or round() for Rounding

Truncation and rounding are different operations. Math.floor() returns the greatest integer less than or equal to the value, while Math.ceil() returns the least integer greater than or equal to it. This difference is especially important for negative values:

const value = -12.75;

console.log(Math.floor(value));
console.log(Math.ceil(value));
console.log(Math.round(-1.5));

Output:

-13
-12
-1

Use Math.floor() for a lower bound, Math.ceil() for an upper bound, and Math.round() when the nearest integer is the intended result. JavaScript’s Math.round() ties toward positive infinity, which is why Math.round(-1.5) returns -1.

Parse Decimal Text with parseInt()

When the source is a string, parseInt() can parse its integer prefix. Pass 10 as the radix for decimal text:

console.log(parseInt("12.75", 10));
console.log(Number.isNaN(parseInt("not a number", 10)));

Output:

12
true

parseInt() has parsing semantics: it can stop when it reaches a character that is not valid for the radix. It is therefore different from truncating an existing number. For example, Math.trunc(Number("12.75")) is appropriate when the input must first be converted to a number and validated as a whole. Use Number.isNaN() or another validation rule when invalid input is possible.

Avoid toFixed() and Bitwise Shortcuts for General Conversion

Number.toFixed() is a formatting method. It rounds to the requested number of decimal places and returns a string, so it is not a general integer-conversion method:

console.log(typeof (12.75).toFixed(0));

Output:

string

Bitwise expressions such as value | 0, ~~value, and value >> 0 also truncate toward zero, but they first convert the value to a signed 32-bit integer. Values outside the signed 32-bit range can wrap around:

console.log(2 ** 31 | 0);

Output:

-2147483648

Use these expressions only when deliberate signed-Int32 coercion is required. They are not safer or clearer replacements for Math.trunc().

Limits to Keep in Mind

Number uses IEEE 754 double-precision floating-point representation. Integers are represented exactly only through Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER; use Number.isSafeInteger() when exact integer arithmetic matters. Also handle non-finite values explicitly:

console.log(Math.trunc(Infinity));
console.log(Math.trunc(NaN));

Output:

Infinity
NaN

Math.trunc() does not make Infinity, -Infinity, or NaN into finite integers. Validate the input when your application requires a finite safe integer.

Summary

For an existing JavaScript number, start with Math.trunc(value) when “integer” means the value with its fractional part removed toward zero. Select Math.floor(), Math.ceil(), or Math.round() when a particular rounding rule is required. Use parseInt(text, 10) for decimal text, remember that toFixed() produces formatted text, and avoid bitwise shortcuts unless signed 32-bit coercion is intentional.