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
🌐
Reddit
reddit.com › r/typescript › trying to create a floating point type?
r/typescript on Reddit: Trying to create a floating point type?
November 13, 2023 -

Hey all, the work I'm currently doing involves me receiving all of my values from some machines as a long string, with all of our variables separated by commas. The problem is that a lot of these values have floating numbers that require a set amount of decimal accuracy, so I can have something like 10.0000 that I would like to maintain as a valid number. Is there any way I can create some sort of type that can maintain the floating zeroes as a value like a string, but be treated as a number? I'm just sick of typing all of our variables with names like somethingNumber: string, it's driving me bananas.

🌐
Code Highlights
code-hl.com › home › javascript › tutorials
5 Easy Ways to Convert JavaScript Float to Int | Code Highlights
June 4, 2024 - All these methods are compatible with major web browsers, including Chrome, Firefox, Safari, Edge, and Internet Explorer. This ensures that your code will run smoothly across different environments. In this tutorial, we explored five simple methods to convert a JavaScript float to an int:
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript float to int
How to Convert a Float Number to Int in JavaScript | Delft Stack
February 2, 2024 - We can apply the bitwise right shift operator (represented by >> characters). It will convert the float value to an integer. Underneath at the binary level, the bitwise right SHIFT operator shifts the binary bits of the operand to the right ...
🌐
Tutorial Teacher
tutorialsteacher.com › typescript › typescript-number
TypeScript number Data Type
In the above example, let first:number = 1; stores a positive integer as a number.let second: number = 0x37CF; stores a hexadecimal as a number which is equivalent to 14287. When you print this number on your browser's console, it prints the equivalent floating point of the hexadecimal number.let third:number=0377; stores an octal number equivalent to 255.
🌐
Internet Computer Developer Forum
forum.dfinity.org › developers
Convert a Float number to an Int type - Developers - Internet Computer Developer Forum
June 10, 2024 - I am trying to convert a float number to an integer. The reason is that I want the E8S equivalent of 1 ICP of a value, for example 1.15. This should be 115_000_000, but the function returns 114_000_000. Other values ​​are calculated correctly, for example 1.25 = 125_000_000 or 0.9 = 90_000_000. public func convertFloatToNat(x: Float): async Int { let ONE_ICP_IN_E8S = 100_000_000; Debug.print("convertFloatToNat "#debug_show(x)); let a = x * 100; Debug.print("float x 100 = "#...
🌐
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 ...
🌐
Byby
byby.dev › js-float-to-int
How to convert float to integer in JavaScript
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.
Find elsewhere
🌐
Webdevtutor
webdevtutor.net › blog › typescript-change-float-to-int
Converting Float to Int in TypeScript: A Comprehensive Guide
One of the simplest ways to convert a float to an integer in TypeScript is by using the Math.floor() method. This method rounds down a number to the nearest integer.
🌐
GitHub
github.com › Microsoft › TypeScript › issues › 21934
Understand 'int', 'integer', 'float', 'double', etc. as 'number' in JSDoc · Issue #21934 · microsoft/TypeScript
December 31, 2017 - All numeric-sounding names should probably resolve to number (maybe only if no other type is defined with such a given name). Ideas: [u]int[8|16|32|64] integer float double
Published   Feb 14, 2018
🌐
GitBook
basarat.gitbook.io › typescript › recap › number
Number | TypeScript Deep Dive
console.log({max: Number.MAX_SAFE_INTEGER, min: Number.MIN_SAFE_INTEGER}); // {max: 9007199254740991, min: -9007199254740991} Safe in this context refers to the fact that the value cannot be the result of a rounding error.
🌐
TypeScript Tutorial
typescripttutorial.net › home › typescript tutorial › typescript number
TypeScript Number
October 18, 2024 - All numbers in TypeScript are either floating-point values or big integers. The floating-point numbers have the type number while the big integers get the type bigint. The following shows how to declare a variable that holds a floating-point value:
🌐
Rip Tutorial
riptutorial.com › float to integer
JavaScript Tutorial => Float to Integer
The floor function returns the first integer less than or equal to the float.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-parse-float-to-int
How to Parse Float to Int in TypeScript
To convert a float value to an integer in TypeScript, you can use the parseInt() function. This function takes a string as its input and returns an integer after parsing the input.
🌐
Atomic Spin
spin.atomicobject.com › 2018 › 11 › 05 › using-an-int-type-in-typescript
Using an Int Type in TypeScript
November 5, 2018 - So even though I knew that the end result would always be a whole number, by forgetting about the inaccuracies of floating point numbers, I let a bug slip through. In order to ensure that the result is always an integer, as required by the PostgreSQL database, it needs to be rounded: ... Forgetting to round the result of a calculation, when you know full well the result should be a whole number, is very easy to do. A teammate suggested that we use TypeScript to force us to round the result by coming up with an Int type we could use whenever a value was going to be inserted into an integer column in the database.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-cast-float-to-int
TypeScript: How to Cast Float to Int
Converting float numbers to integers in TypeScript is a common requirement in many applications. By using methods like Math.floor() and type assertions, you can easily cast float numbers to ints in TypeScript.
🌐
Luis Llamas
luisllamas.es › inicio › cursos › curso typescript
The Number Type in TypeScript
June 23, 2025 - TypeScript provides several ways to do this, including parseInt, parseFloat, and the unary + operator. let string: string = "42"; let integer: number = parseInt(string); // 42 let float: number = parseFloat("3.14"); // 3.14 let number: number ...
🌐
Medium
medium.com › interesting-coding › all-typescript-types-you-need-to-know-dd1423fd6687
All TypeScript Types You Need To Know | by Cihan | Interesting Coding | Medium
November 10, 2023 - In TypeScript, there are several built-in types that can be used to specify the type of a variable, function parameter, or function return value. These include: boolean: Represents a value that is either true or false. number: Represents a numeric value, either integer or floating-point.