JavaScript only has a Number type that stores floating point values.

There is no int.

Edit:

If you want to format the number as a string with two digits after the decimal point use:

(4).toFixed(2)
Answer from Alex Jasmin on Stack Overflow
🌐
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.

Discussions

[SOLVED] typecast float to int in javascript? - Mobile - Unity Discussions
A forum search has lead me to think it’s not doable. “as” only works on reference types and Unity doesn’t dig var f1 : float = 3.14; var myVar : int = (int)f1; or var f1 : float = 3.14; var myVar : int = int(f1); If anyone knows a typecast method that work’s I’d appreciate a “how ... More on discussions.unity.com
🌐 discussions.unity.com
1
September 18, 2009
syntax - How do I convert a float number to a whole number in JavaScript? - Stack Overflow
Therefore using bitwise operations on floating point values will convert them to an integer stripping off digits after decimal point. 2013-09-02T12:22:21.963Z+00:00 ... If you just need it for positive numbers, Math.floor() is faster (at least according to my running of your first JSPerf test on ... More on stackoverflow.com
🌐 stackoverflow.com
Hey JavaScript, can you turn my float into int properly?
For those who haven’t seen this, it’s because parseInt() takes a string argument. 0.0000005 automatically typecasts to the string ‘5e-7’ in scientific notation. parseInt sees the 5, uses it, sees the e which is not a decimal number, and stops. If you do parseInt(0.0000005, 16) to parse the string in base 16, the answer becomes 94 instead of 5 because e is also a hex digit. More on reddit.com
🌐 r/ProgrammerAnimemes
86
1701
February 3, 2022
How do you cast an int to a float?
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… More on forums.unrealengine.com
🌐 forums.unrealengine.com
7
2
March 27, 2023
🌐
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...
🌐
W3Schools
w3schools.com › js › js_numbers.asp
JavaScript Numbers
Javascript numbers are always double (64-bit floating point). Integers (numbers without a period or exponent notation) are accurate up to 15 digits.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number
Number - JavaScript | MDN
Number values represent floating-point numbers like 37 or -9.25. The Number constructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using the Number() function.
🌐
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.
Find elsewhere
🌐
Unity
discussions.unity.com › target platforms › mobile
[SOLVED] typecast float to int in javascript? - Mobile - Unity Discussions
September 18, 2009 - A forum search has lead me to think it’s not doable. “as” only works on reference types and Unity doesn’t dig var f1 : float = 3.14; var myVar : int = (int)f1; or var f1 : float = 3.14; var myVar : int = int(f1);…
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › convert-int-to-float
Convert int to float - GeeksforGeeks
March 26, 2024 - In Explicit Type Conversion,we manually convert one data type to another using built-in functions such as int(), float(), str(), etc.
Top answer
1 of 16
2263
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
🌐
TutorialsPoint
tutorialspoint.com › article › 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?
March 15, 2026 - In this tutorial, we will learn to convert a float number to a whole number in JavaScript. The whole number is the number that doesn't contain the fractional part. For example, 5.22 is the float number containing the fractional part, and the whole number doesn't contain a decimal part.
🌐
Just Academy
justacademy.co › blog-detail › how-to-convert-float-to-int-in-javascript
How To Convert Float To Int In JavaScript by Roshan Chaturvedi | JustAcademy
Converting float numbers to integers in JavaScript Written by Roshan Chaturvedi. How To Convert Float To Int In JavaScriptConverting a float to an int in JavaScript is useful when you want to remove the decimal part of a number and work with whole numbers only.
🌐
W3Schools
w3schools.com › js › js_number_methods.asp
JavaScript Number Methods
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Certificate ... The toString() method returns a number as a string.
🌐
Reddit
reddit.com › r/programmeranimemes › hey javascript, can you turn my float into int properly?
[Mature Content] r/ProgrammerAnimemes on Reddit: Hey JavaScript, can you turn my float into int properly?
February 3, 2022 - I haven’t actually tried that (I’m on my phone) but casting to a number and then making that an integer seems the most straightforward. Plus it’s not limited to 31 bits (try 3_000_000_000 in yours) ... Javascript dont has int or float type, all they has is number type and bignumber, so ...
🌐
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
Not supported in Internet Explorer. Alternatively, you can also try other methods like floor(), ceil() and round() to round a number.
🌐
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…
🌐
Facebook
facebook.com › groups › learanHTMLandCSS › posts › 3003604959968382
Converting int to float without converting to string?
Popular groups · Find communities for you · Over 1 billion people across the globe are using Facebook Groups to explore their favorite topics · Log in · Categories · Science & tech · Travel · Animals · Sports & fitness · Entertainment
🌐
Codemia
codemia.io › home › knowledge hub › 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? | Codemia
December 28, 2024 - Over 8,000 real interview questions from top companies, searchable by company and role.Browse interview questions ... In JavaScript, converting a floating-point number (a number that has a decimal point) to a whole number is a common task that can be completed in several ways, each with its own implications and use cases.
🌐
CalculatorSoup
calculatorsoup.com › calculators › math › decimal-to-fraction-calculator.php
Decimal to Fraction Calculator
Convert decimals to fractions or mixed number fractions. Calculator to change decimals to fractions showing the work with steps. Converts repeating decimals to fractions.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-check-if-a-number-is-float-or-integer
JavaScript Program to Check if a Number is Float or Integer - GeeksforGeeks
July 23, 2025 - The Modulus Operator calculates the remainder when the number is divided by 1. If the remainder is 0, the number is an integer; otherwise, it's a float. ... Example: This example uses the Modulus Operator (%) to check the Number.