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
🌐
W3Schools
w3schools.com › js › js_numbers.asp
JavaScript Numbers
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 ...
Discussions

[AskJS] Its about time javascript should get a "integer" and "float" data type.
JavaScript numbers are double-precision floating-point values, also known as a double in other languages like C. This covers the "float" side of things. Now, a double-precision floating-point value can perfectly represent integers up to 52 bits, which is a lot bigger than the 32 bits an int can typically store in other languages. JavaScript bitwise operators such as & or | or ^ also convert their values to 32-bit integers before doing their work. So, just use regular JavaScript numbers to perform your integer arithmetic, and then throw in a few | 0 operations if you want the rounding / clamping behavior found in other language's 32-bit int types. In other words, everything you want is already present. More on reddit.com
🌐 r/javascript
46
0
August 10, 2022
Convert a Float number to an Int type
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. ... More on forum.dfinity.org
🌐 forum.dfinity.org
0
June 10, 2024
Does JavaScript supports conversion of integer X to X.0 as typeof number?
This doesn't really make sense in context of JavaScript. As far as JS is concerned, the numbers 5 and 5.0 are exactly the same. In context of JSON, the numbers are also exactly the same. Hence, you can't really "make 5 into 5.0" because it already is, and the fact the decimals are not displayed is just how the number happens to be formatted by the particular JS engine that's displaying it to you. If you have a bizarre API which refuses to accept numbers such as 5, but won't accept a string '5.0', you will probably have to format the values yourself. Eg. use whatever.toFixed(1), which gives you a string, and then manually strip out the string delimiters from the JSON data. You might be able to do this using the optional replacer function for JSON.stringify - just have it check if the value is typeof 'number', and ensure it always uses toFixed when formatting them. More on reddit.com
🌐 r/learnjavascript
41
21
January 1, 2023
javascript convert int to float - Stack Overflow
I have a variable var fval = 4; now I want out put as 4.00 More on stackoverflow.com
🌐 stackoverflow.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseFloat
parseFloat() - JavaScript | MDN
A floating point number parsed from the given string, or NaN when the first non-whitespace character cannot be converted to a number. Note: JavaScript does not have the distinction of "floating point numbers" and "integers" on the language level. parseInt() and parseFloat() only differ in their parsing behavior, but not necessarily their return values.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-convert-a-float-number-to-a-whole-number-in-javascript.php
How to Convert a Float Number to a Whole Number in JavaScript
You can use the trunc() method to get the integer part of a number by removing any fractional digits. This method was added in ECMAScript 6 and supported by the latest browsers such as Chrome, Firefox, Safari, Opera, and Edge browsers.
🌐
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?
July 20, 2022 - 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 ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › parseFloat
Number.parseFloat() - JavaScript | MDN
The Number.parseFloat() static method parses an argument and returns a floating point number. If a number cannot be parsed from the argument, it returns NaN. function circumference(r) { if (Number.isNaN(Number.parseFloat(r))) { return 0; } return parseFloat(r) * 2.0 * Math.PI; } console.lo...
🌐
Reddit
reddit.com › r/javascript › [askjs] its about time javascript should get a "integer" and "float" data type.
r/javascript on Reddit: [AskJS] Its about time javascript should get a "integer" and "float" data type.
August 10, 2022 -

After all the improvements/features and fixes which were made to javascript (or precisely ECMAScript) since ES6 (ES2015) till now (ES2022), isn't it about time to fix the ultimate pending weakness i.e. having an Integer and a Float type as well (just like "var" keyword was/is there but they added "let" and "const" as a better semantics and good practice) and not just mashup everything in Number.

Heck, we even got a big int to represent big integers (no floats allowed) but we still don't have Integer's and Floats which are SUPER useful in almost every scenario.

So, why is it still not added and not planned as well? Those are VERY important data types and MOST languages HAVE those data types as they are NEEDED, why is it not planned for ECMAScript? Is it planned? Do you want to see this added?

Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › 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
July 11, 2025 - ... // float value is 4.59; let x = 4.59; let z = Math.round(x); console.log("Converted value of " + x + " is " + z); ... Return the integer part of a floating-point number by removing the fractional digits.
🌐
Reasonml-old
reasonml-old.github.io › guide › language › integer-and-float
Integer & Float
Careful when you bind to JavaScript numbers! Long ones might be truncated. Bind JS number as float instead.
🌐
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
🌐
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 = "#...
🌐
W3Schools
w3schools.com › jsref › jsref_parsefloat.asp
JavaScript parseFloat() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST
🌐
TutorialsTeacher
tutorialsteacher.com › javascript › javascript-number
JavaScript Numers: Integer, Float, Binary, Exponential, Hexadecimal, Octal
Append n to the end of an integer to make it BigInt. ... //16 digit integer var int1 = 1234567890123459n; //will be 1234567890123459 //17 digit integer var int2 = 12345678901234569n; //will be 12345678901234569 //20 digit integer var int3 = ...
🌐
Reddit
reddit.com › r/learnjavascript › does javascript supports conversion of integer x to x.0 as typeof number?
r/learnjavascript on Reddit: Does JavaScript supports conversion of integer X to X.0 as typeof number?
January 1, 2023 -

Given string lets say “5” I want to convert it to 5.0 (typeof Number)

Already tried combinations/permutations of all the following: parseFloat() toFixed() toPrecision() Number() Also tried with “+” (implicit Number casting)

I always end up with either typeof string Or just the number 5

Its for an API that expects “float” but does not accept integers (5 is invalid, 5.0 is valid)

Any ideas would be appreciated.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number
Number - JavaScript | MDN
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.
🌐
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 › toFixed
Number.prototype.toFixed() - JavaScript | MDN
The toFixed() method of Number values returns a string representing this number using fixed-point notation with the specified number of decimal places.
🌐
M-ou
blog.m-ou.se › floats
Converting Integers to Floats Using Hyperfocus - Mara's Blog
May 31, 2022 - Now we can continue with the fun part: every other integer. As the input is unsigned, we already know one bit: the sign bit is always zero. One bit down, sixty-three left! let sign = 0; let exponent = ??; // TODO let mantissa = ??; // TODO let bits: u64 = (sign << 63) + (exponent << 52) + mantissa; Next up: the exponent. For this we need to know the position of the highest 1 bit, and add 1023 because that’s how floats work.
🌐
Unreal Engine
forums.unrealengine.com › development › programming & scripting › verse
How do you cast an int to a float? - Verse - Epic Developer Community Forums
March 27, 2023 - I have tried absolutely everything and cannot seem to cast an integer to a float. The closest I got is if (test := float[float_val]) {} Which displays the extremely odd error Dynamic cast to `float` takes an float as its parameter, instead got int(3509) Anybody have any ideas?