var intvalue = Math.floor( floatvalue );
var intvalue = Math.ceil( floatvalue ); 
var intvalue = Math.round( floatvalue );

// `Math.trunc` was added in ECMAScript 6
var intvalue = Math.trunc( floatvalue );

Math object reference


Examples

Positive
// value=x        //  x=5          5<x<5.5      5.5<=x<6  

Math.floor(value) //  5            5            5
Math.ceil(value)  //  5            6            6
Math.round(value) //  5            5            6
Math.trunc(value) //  5            5            5
parseInt(value)   //  5            5            5
~~value           //  5            5            5
value | 0         //  5            5            5
value >> 0        //  5            5            5
value >>> 0       //  5            5            5
value - value % 1 //  5            5            5
Negative
// value=x        // x=-5         -5>x>=-5.5   -5.5>x>-6

Math.floor(value) // -5           -6           -6
Math.ceil(value)  // -5           -5           -5
Math.round(value) // -5           -5           -6
Math.trunc(value) // -5           -5           -5
parseInt(value)   // -5           -5           -5
value | 0         // -5           -5           -5
~~value           // -5           -5           -5
value >> 0        // -5           -5           -5
value >>> 0       // 4294967291   4294967291   4294967291
value - value % 1 // -5           -5           -5
Positive - Larger numbers
// x = Number.MAX_SAFE_INTEGER/10 // =900719925474099.1

// value=x            x=900719925474099    x=900719925474099.4  x=900719925474099.5
           
Math.floor(value) //  900719925474099      900719925474099      900719925474099
Math.ceil(value)  //  900719925474099      900719925474100      900719925474100
Math.round(value) //  900719925474099      900719925474099      900719925474100
Math.trunc(value) //  900719925474099      900719925474099      900719925474099
parseInt(value)   //  900719925474099      900719925474099      900719925474099
value | 0         //  858993459            858993459            858993459
~~value           //  858993459            858993459            858993459
value >> 0        //  858993459            858993459            858993459
value >>> 0       //  858993459            858993459            858993459
value - value % 1 //  900719925474099      900719925474099      900719925474099
Negative - Larger numbers
// x = Number.MAX_SAFE_INTEGER/10 * -1 // -900719925474099.1

// value = x      // x=-900719925474099   x=-900719925474099.5 x=-900719925474099.6

Math.floor(value) // -900719925474099     -900719925474100     -900719925474100
Math.ceil(value)  // -900719925474099     -900719925474099     -900719925474099
Math.round(value) // -900719925474099     -900719925474099     -900719925474100
Math.trunc(value) // -900719925474099     -900719925474099     -900719925474099
parseInt(value)   // -900719925474099     -900719925474099     -900719925474099
value | 0         // -858993459           -858993459           -858993459
~~value           // -858993459           -858993459           -858993459
value >> 0        // -858993459           -858993459           -858993459
value >>> 0       //  3435973837           3435973837           3435973837
value - value % 1 // -900719925474099     -900719925474099     -900719925474099
Answer from moonshadow on Stack Overflow
Top answer
1 of 16
2261
var intvalue = Math.floor( floatvalue );
var intvalue = Math.ceil( floatvalue ); 
var intvalue = Math.round( floatvalue );

// `Math.trunc` was added in ECMAScript 6
var intvalue = Math.trunc( floatvalue );

Math object reference


Examples

Positive
// value=x        //  x=5          5<x<5.5      5.5<=x<6  

Math.floor(value) //  5            5            5
Math.ceil(value)  //  5            6            6
Math.round(value) //  5            5            6
Math.trunc(value) //  5            5            5
parseInt(value)   //  5            5            5
~~value           //  5            5            5
value | 0         //  5            5            5
value >> 0        //  5            5            5
value >>> 0       //  5            5            5
value - value % 1 //  5            5            5
Negative
// value=x        // x=-5         -5>x>=-5.5   -5.5>x>-6

Math.floor(value) // -5           -6           -6
Math.ceil(value)  // -5           -5           -5
Math.round(value) // -5           -5           -6
Math.trunc(value) // -5           -5           -5
parseInt(value)   // -5           -5           -5
value | 0         // -5           -5           -5
~~value           // -5           -5           -5
value >> 0        // -5           -5           -5
value >>> 0       // 4294967291   4294967291   4294967291
value - value % 1 // -5           -5           -5
Positive - Larger numbers
// x = Number.MAX_SAFE_INTEGER/10 // =900719925474099.1

// value=x            x=900719925474099    x=900719925474099.4  x=900719925474099.5
           
Math.floor(value) //  900719925474099      900719925474099      900719925474099
Math.ceil(value)  //  900719925474099      900719925474100      900719925474100
Math.round(value) //  900719925474099      900719925474099      900719925474100
Math.trunc(value) //  900719925474099      900719925474099      900719925474099
parseInt(value)   //  900719925474099      900719925474099      900719925474099
value | 0         //  858993459            858993459            858993459
~~value           //  858993459            858993459            858993459
value >> 0        //  858993459            858993459            858993459
value >>> 0       //  858993459            858993459            858993459
value - value % 1 //  900719925474099      900719925474099      900719925474099
Negative - Larger numbers
// x = Number.MAX_SAFE_INTEGER/10 * -1 // -900719925474099.1

// value = x      // x=-900719925474099   x=-900719925474099.5 x=-900719925474099.6

Math.floor(value) // -900719925474099     -900719925474100     -900719925474100
Math.ceil(value)  // -900719925474099     -900719925474099     -900719925474099
Math.round(value) // -900719925474099     -900719925474099     -900719925474100
Math.trunc(value) // -900719925474099     -900719925474099     -900719925474099
parseInt(value)   // -900719925474099     -900719925474099     -900719925474099
value | 0         // -858993459           -858993459           -858993459
~~value           // -858993459           -858993459           -858993459
value >> 0        // -858993459           -858993459           -858993459
value >>> 0       //  3435973837           3435973837           3435973837
value - value % 1 // -900719925474099     -900719925474099     -900719925474099
2 of 16
348

Bitwise OR operator

A bitwise or operator can be used to truncate floating point figures and it works for positives as well as negatives:

function float2int (value) {
    return value | 0;
}

Results

float2int(3.1) == 3
float2int(-3.1) == -3
float2int(3.9) == 3
float2int(-3.9) == -3

Performance comparison?

I've created a JSPerf test that compares performance between:

  • Math.floor(val)
  • val | 0 bitwise OR
  • ~~val bitwise NOT
  • parseInt(val)

that only works with positive numbers. In this case you're safe to use bitwise operations well as Math.floor function.

But if you need your code to work with positives as well as negatives, then a bitwise operation is the fastest (OR being the preferred one). This other JSPerf test compares the same where it's pretty obvious that because of the additional sign checking Math is now the slowest of the four.

Note

As stated in comments, BITWISE operators operate on signed 32bit integers, therefore large numbers will be converted, example:

1234567890  | 0 => 1234567890
12345678901 | 0 => -539222987
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-a-float-number-to-the-whole-number-in-javascript
How to Convert a Float Number to the Whole Number in JavaScript? | GeeksforGeeks
September 16, 2024 - Below are various methods to convert float numbers to whole numbers in JavaScript: ... Round off the number passed as a parameter to its nearest integer in the Downward direction.
🌐
UsefulAngle
usefulangle.com › post › 238 › javascript-get-integer-from-decimal-float
Get Integer Value from a Decimal Number in Javascript
December 19, 2019 - The number can also be rounded to the next integer using Math.round(), Math.ceil() and Math.floor() methods. The Math.trunc() method simply truncates the decimal part of the number and returns the integer part.
🌐
TutorialsPoint
tutorialspoint.com › how-do-i-convert-a-float-number-to-a-whole-number-in-javascript
How do I convert a float number to a whole number in JavaScript?
In the above output users can see that Math.trunc() method has removed the decimal part and Math.round() method has rounded the number to nearest integer. In this approach, we will use the parseInt() method. The parseInt() method extracts the whole number from the float number or string in ...
🌐
Educative
educative.io › answers › how-to-convert-string-to-int-or-number-in-javascript
How to convert string to int or number in JavaScript
In JavaScript, the double tilde (~~) operator can be used to convert a string to a number, but this method will truncate any decimal portions if the input string contains a decimal value.
🌐
Byby
byby.dev › js-float-to-int
How to convert float to integer in JavaScript
Converting a float to an integer is a common operation that allows you to work with whole numbers instead of decimal values. You can use various methods that either round, truncate, or parse the float value. Depending on your specific use case, you can choose the most appropriate method: Using Math.trunc(). This method returns the integer part of a number by removing any fractional digits.
🌐
Quora
quora.com › Given-a-number-with-decimals-in-JavaScript-how-do-I-unpack-its-decimals-and-its-integers-into-two-different-variables
Given a number with decimals in JavaScript, how do I unpack its decimals and its integers into two different variables? - Quora
Answer (1 of 9): Think about this a minute - That’s the problem w/ Quora programming question askers lol - Not thinking about the questions prior to asking. Let’s say you have a number 6.23. Now you want to break that apart if I understood you right into 2 variables, one with 6 and the ...
🌐
W3Schools
w3schools.com › js › js_number_methods.asp
JavaScript Number Methods
Safe integers are all integers from -(253 - 1) to +(253 - 1). This is safe: 9007199254740991. This is not safe: 9007199254740992. For a complete reference to all JavaScript properties and methods, with full descriptions and many examples, go to:
🌐
Mikemcl
mikemcl.github.io › decimal.js
decimal.js API
number: integer, -9e15 to 0 inclusive Default value: -9e15 · The negative exponent limit, i.e. the exponent value below which underflow to zero occurs. If the Decimal to be returned by a calculation would have an exponent lower than minE then the value of that Decimal becomes zero. JavaScript numbers underflow to zero for exponents below -324.
Find elsewhere
🌐
Codemia
codemia.io › knowledge-hub › path › how_do_i_convert_a_float_number_to_a_whole_number_in_javascript
How do I convert a float number to a whole ...
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
W3Schools
w3schools.com › jsref › jsref_parseint.asp
JavaScript parseInt() Method
The parseInt method parses a value as a string and returns the first integer. A radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal. If radix is omitted, JavaScript assumes radix 10.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseInt
parseInt() - JavaScript | MDN
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number
Number - JavaScript | MDN
255; // two-hundred and fifty-five 255.0; // same number 255 === 255.0; // true 255 === 0xff; // true (hexadecimal notation) 255 === 0b11111111; // true (binary notation) 255 === 0.255e3; // true (decimal exponential notation) A number literal like 37 in JavaScript code is a floating-point value, not an integer. There is no separate integer type in common everyday use. (JavaScript also has a BigInt type, but it's not designed to replace Number for everyday uses.
🌐
W3Schools
w3schools.com › js › js_numbers.asp
JavaScript Numbers
let x = 3.14; // A number with decimals let y = 3; // A number without decimals ... Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc. JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard. This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63:
🌐
W3Schools
w3schools.com › jsref › jsref_tofixed.asp
JavaScript toFixed() Method
If the number of decimals are higher than in the number, zeros are added. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › toFixed
Number.prototype.toFixed() - JavaScript | MDN
The toFixed() method returns a string representation of a number without using exponential notation and with exactly digits digits after the decimal point.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Numbers
December 18, 2024 - And if we place one more dot, then JavaScript knows that the decimal part is empty and now uses the method. Also could write (123456).toString(36). One of the most used operations when working with numbers is rounding. There are several built-in functions for rounding: ... Rounds down: 3.1 becomes 3, and -1.1 becomes -2. ... Rounds up: 3.1 becomes 4, and -1.1 becomes -1. ... Rounds to the nearest integer: 3.1 becomes 3, 3.6 becomes 4.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Math › round
JavaScript Math round() - Round Number to Integer | Vultr Docs
December 2, 2024 - The Math.round() function in JavaScript is a method used to round a number to the nearest integer. This method is essential when you need to eliminate the decimal parts of numbers during calculations, which is particularly useful in financial ...
🌐
Scaler
scaler.com › home › topics › convert string to number in javascript
String to Number Javascript | Scaler Topics
January 6, 2024 - The parseInt function in Javascript takes a string and converts it into an integer value. ... radix : The radix is used to represent the base of the number into which the output will be displayed.
🌐
Code Highlights
code-hl.com › home › javascript › tutorials
5 Easy Ways to Convert JavaScript Float to Int | Code Highlights
June 4, 2024 - parseInt(): Parses a string and returns an integer. Bitwise OR (|): Truncates the decimal part. Understanding these methods helps in handling numerical data more effectively. For further learning, consider exploring our JavaScript course or our Introduction to Web Development course.