Converting Floats to Integers in JavaScript: 10 Effective Ways

Find out the different methods to achieve JavaScript float to int conversion effectively.

Do you want to know how to convert float numbers to integers in JavaScript? All you have to do is keep reading this comprehensive guide.

But before we explore into the solutions of converting floats to integers. Let’s first understand the floating point numbers and integers.

What are floating point numbers?

Floating-point numbers, commonly known as floats, are numbers that contain a fractional part.

In JavaScript, these numbers are represented using the number data type and can have decimals.

For example, 4.14 and 0.75 are floating-point numbers

What are Integers?

Integers are whole numbers without any fractional parts. They are also represented using the number data type in JavaScript.

Examples of integers include 8, 10, and -16.

How to convert a float number to a whole number in JavaScript?

JavaScript offers several methods to convert float to int, each with its own advantages and use cases.

Let’s explore these methods:

Use parseInt()

One of the simplest ways to convert a float to an int is by using the parseInt() function. This function parses a string argument and returns an integer.

Syntax

parseInt(string, radix(optional))

To convert a float, we can pass the floating-point number as a string to the parseInt() function.

For example:

let floatValue = 1.14;
let intValue = parseInt(floatValue);
console.log(intValue);

Output:

1

Use Math.floor()

The Math.floor() function rounds down a float to the nearest integer, making it an effective method for converting floats to integers.

Syntax

Math.floor(number)

Here’s an example:

let floatValue = 2.99;
let intValue = Math.floor(floatValue);
console.log(intValue);

Output:

2

Use Math.ceil()

The Math.ceil() function rounds up a float to the nearest integer, which can be helpful in certain situations.

Syntax

Math.ceil(val)

Here’s an example:

let floatValue = 2.5;
let intValue = Math.ceil(floatValue);
console.log(intValue);

Output:

3

Use Math.round()

The Math.round() function rounds a float to the nearest integer. If the decimal part is .5 or greater, it rounds up; otherwise, it rounds down.

Syntax

Math.round(number)

Here’s an example:

let floatValue = 3.9;
let intValue = Math.round(floatValue);
console.log(intValue); 

Output:

4

Here’s another example:

let floatValue = 3.3;
let intValue = Math.round(floatValue);
console.log(intValue); 

Output:

3

Use Bitwise OR (|) Operator

Another unique approach to convert float to int involves using the bitwise OR operator (|). This method truncates the decimal part and gives the integer value.

Syntax

number | 0

Here’s an example:

let floatValue = 5.8;
let intValue = floatValue | 0;
console.log(intValue);

Output:

5

Use Math.trunc()

This function removes any fractional digits and returns the integer part of a number.

Syntax

Math.trunc(number)

Here’s an example:

let num = 6.7;
let result = Math.trunc(num);
console.log(result);

Output:

6

Use Bitwise NOT (~~)

This operator inverts all the bits in your number and in the process converts the number to an int!

Syntax

~~number

Here’s an example:

let num = 7.7;
let result = ~~num;
console.log(result);

Output:

7

Use Bitwise Right Shift (>>)

This operator shifts the bits of the first operand to the right by the number of positions specified by the second operand and returns a result of the same type as the first operand.

Syntax

 number >> 0

Here’s an example:

let num = 8.8;
let result = num >> 0;
console.log(result); 

Output:

8

Use Unsigned Right Shift (>>>)

This operator shifts the bits of the first operand to the right by the number of positions specified by the second operand and returns a result of the same type as the first operand, but with all leading zeros filled with zeros instead of ones (as in a signed right shift).

Syntax

 number >>> 0

Here’s an example:

let num = 9.99;
let result = num >>> 0;
console.log(result);

Output:

9

Use Modulo (%)

This operator returns the remainder of dividing two numbers. You can use it to remove any fractional part from a float by subtracting it from itself modulo 1: (x – x % 1).

Syntax

 (number - number % 1)

Here’s an example:

let num = 10.10;
let result = (num - num % 1);
console.log(result);

Output:

10

Dealing with rounding errors in floating-point conversions

When working with floating-point numbers, it is important to be aware of rounding errors that may occur during conversions.

These errors are due to the inherent imprecision of floating-point representations.

To mitigate such errors, consider using the toFixed() method or custom rounding functions that adjust the number of decimal places before conversion.

Here’s an example code:

let floatValue = 1.2345;
let roundedValue = floatValue.toFixed(2); // Rounded to 2 decimal places
let intValue = parseInt(roundedValue);
console.log(intValue); 

Output:

1

Conclusion

In conclusion, this article provides various methods for converting float numbers to integers in JavaScript.

We discussed the concepts of floating-point numbers and integers and provided the different effective methods along with example codes and output for better understanding when converting data types.

We are hoping that this article provides you with enough information that help you understand the JavaScript float to int.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment