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
🌐
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.
🌐
Supabase
supabase.com › docs › guides › database › tables
Tables and Data | Supabase Docs
2 days ago - There are several ways to load data in Supabase. You can load data directly into the database or using the APIs.
Discussions

[SOLVED] typecast float to int in javascript? - Unity Engine - 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
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I'd like to convert a float to a whole number in JavaScript. Actually, I'd like to know how to do BOTH of the standard conversions: by truncating and by rounding. More on stackoverflow.com
🌐 stackoverflow.com
- Post Your Cockpit Pictures Here - - Page 109 - Home Cockpit Builders - Microsoft Flight Simulator Forums
It is. And it works really well. My only caution is that the twist and button-press action of the levers seems a little thin, like it will wear out with a lot of repeated use. Oh, forgot to mention the flap lever is really nice, too - works as both a momentary button and/or an axis. More on forums.flightsimulator.com
🌐 forums.flightsimulator.com
3 weeks ago
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
🌐
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 ...
🌐
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.
🌐
Quora
quora.com › How-do-I-convert-any-float-number-to-integer-in-c
How to convert any float number to integer in c - Quora
Answer (1 of 23): First, if the non-fractional part of a [code ]float[/code] is representable as an [code ]int[/code], an implicit conversion that “rounds toward zero” (i.e., drops the fractional) exists: [code]int r(float f) { int result = f; return result; // You could just write "return ...
🌐
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.
🌐
Unity
discussions.unity.com › unity engine
[SOLVED] typecast float to int in javascript? - Unity Engine - 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);…
Find elsewhere
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 › javascript › javascript-program-to-convert-float-to-string
JavaScript Program to Convert Float to String - GeeksforGeeks
August 5, 2025 - The String() function is one of the most commonly used and simplest ways to convert a float to a string. It works by converting the given value into its string representation. ... String(num) converts the float num into a string. This method is simple and widely supported for basic type conversion. Every JavaScript object has the toString() method, which returns a string representing the object.
🌐
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
🌐
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and Output — Python 3.14.3 documentation
Strings can easily be written to and read from a file. Numbers take a bit more effort, since the read() method only returns strings, which will have to be passed to a function like int(), which takes a string like '123' and returns its numeric value 123.
🌐
PyPI
pypi.org › project › fastapi
fastapi · PyPI
Conversion of input data: coming from the network to Python data and types. Reading from: ... Path parameters. Query parameters. ... Convert Python types (str, int, float, bool, list, etc).
      » pip install fastapi
    
Published   Mar 01, 2026
Version   0.135.1
🌐
Ollama
docs.ollama.com › capabilities › structured-outputs
Structured Outputs - Ollama
from ollama import chat from pydantic import BaseModel from typing import Literal, Optional class Object(BaseModel): name: str confidence: float attributes: str class ImageDescription(BaseModel): summary: str objects: list[Object] scene: str colors: list[str] time_of_day: Literal['Morning', 'Afternoon', 'Evening', 'Night'] setting: Literal['Indoor', 'Outdoor', 'Unknown'] text_content: Optional[str] = None response = chat( model='gemma3', messages=[{ 'role': 'user', 'content': 'Describe this photo and list the objects you detect.', 'images': ['path/to/image.jpg'], }], format=ImageDescription.model_json_schema(), options={'temperature': 0}, ) image_description = ImageDescription.model_validate_json(response.message.content) print(image_description)
🌐
NASA
api.nasa.gov
NASA Open APIs
Generate API Key · Loading signup form · Please enable JavaScript to signup for an api.data.gov API key
🌐
Swagger
swagger.io › specification
OpenAPI Specification - Version 3.1.0 | Swagger
The OpenAPI Specification defines a standard interface to RESTful APIs which allows both humans and computers to understand service capabilities without access to source code, documentation, or network traffic inspection.
🌐
Microsoft Flight Simulator Forums
forums.flightsimulator.com › discussion hub › home cockpit builders
- Post Your Cockpit Pictures Here - - Page 109 - Home Cockpit Builders - Microsoft Flight Simulator Forums
3 weeks ago - It is. And it works really well. My only caution is that the twist and button-press action of the levers seems a little thin, like it will wear out with a lot of repeated use. Oh, forgot to mention the flap lever is really nice, too - works as both a momentary button and/or an axis.
🌐
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 ...
🌐
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 = "#...