my_var = (int)my_var;

As simple as that. Basically you don't need it if the variable is int.

Answer from Zach P on Stack Overflow
🌐
VisibleEarth
h-schmidt.net › FloatConverter › IEEE754.html
IEEE-754 Floating Point Converter
Note: The converter used to show denormalized exponents as 2-127 and a denormalized mantissa range [0:2). This is effectively identical to the values above, with a factor of two shifted between exponent and mantissa. However this confused people and was therefore changed (2015-09-26). ... Not every decimal number can be expressed exactly as a floating point number. This can be seen when entering "0.1" and examining its binary representation which is either slightly smaller or larger, depending on the last bit. ... The hex representation is just the integer value of the bitstring printed as hex.
Discussions

casting - Converting a float to an integer - Stack Overflow
There is a catch! I have a IEEE 754 single precision (32 bit) float stored in two consecutive 16bit integers. The processor I am using doesn't have floating point maths or float data types! What ... More on stackoverflow.com
🌐 stackoverflow.com
floating point - How to convert float to int with Java - Stack Overflow
I used the following line to convert float to int, but it's not as accurate as I'd like: float a=8.61f; int b; b=(int)a; The result is : 8 (It should be 9) When a = -7.65f, the result is : -7... More on stackoverflow.com
🌐 stackoverflow.com
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
0
June 10, 2024
C - Calculating and printing int as float - Stack Overflow
You can also use %n.nf to print the number of decimals you want to. In the above example 2 decimals. ... you cannot store a float value in an integer one because the decimal part will be discarded, converted implicitly to an integer so you have to declare f as float or double or float: More on stackoverflow.com
🌐 stackoverflow.com
🌐
Binary Convert
binaryconvert.com › convert_float.html
Float (IEEE754 Single precision 32-bit) Converter
Online binary converter. Supports all types of variables, including single and double precision IEEE754 numbers
🌐
Stack Overflow
stackoverflow.com › questions › 9392506 › converting-a-float-to-an-integer
casting - Converting a float to an integer - Stack Overflow
The integer will also need some implied scaling factor based upon the value ranges in question. Here's a simple example to make things clearer. Say the float has a range of 0.00 to 10.00. In this case I want the integer to range from 0 to 1000. Note the implied scaling factor of 100.
Top answer
1 of 9
798

Using Math.round() will round the float to the nearest integer.

2 of 9
210

Actually, there are different ways to downcast float to int, depending on the result you want to achieve: (for int i, float f)

  • round (the closest integer to given float)

    i = Math.round(f);
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  2 ; f =  2.68 -> i =  3
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -3
    

    note: this is, by contract, equal to (int) Math.floor(f + 0.5f)

  • truncate (i.e. drop everything after the decimal dot)

    i = (int) f;
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  2 ; f =  2.68 -> i =  2
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -2
    
  • ceil/floor (an integer always bigger/smaller than a given value if it has any fractional part)

    i = (int) Math.ceil(f);
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  3 ; f =  2.68 -> i =  3
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -2
    
    i = (int) Math.floor(f);
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  2 ; f =  2.68 -> i =  2
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -3 ; f = -2.68 -> i = -3
    

For rounding positive values, you can also just use (int)(f + 0.5), which works exactly as Math.Round in those cases (as per doc).

You can also use Math.rint(f) to do the rounding to the nearest even integer; it's arguably useful if you expect to deal with a lot of floats with fractional part strictly equal to .5 (note the possible IEEE rounding issues), and want to keep the average of the set in place; you'll introduce another bias, where even numbers will be more common than odd, though.

See

http://mindprod.com/jgloss/round.html

http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html

for more information and some examples.

🌐
Evanau
flop.evanau.dev
Float Converter
Floating-point converter for FP32, FP64, FP16, bfloat16, TensorFloat-32 and arbitrary IEEE 754-style floating-point types.
🌐
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 = "#...
Find elsewhere
🌐
Replit
replit.com › home › discover › how to convert a float to an int in python
How to convert a float to an int in Python
February 6, 2026 - from decimal import Decimal result = Decimal('0.1') + Decimal('0.2') integer_test = int(result * 10) print(f"0.1 + 0.2 = {result}") print(f"(0.1 + 0.2) * 10 as integer: {integer_test}") To sidestep floating-point inaccuracies, you can use Python's Decimal module. By initializing numbers as strings, like Decimal('0.1'), you preserve their exact decimal representation. This prevents the small errors that binary floats introduce, ensuring calculations like Decimal('0.1') + Decimal('0.2') yield exactly 0.3.
🌐
Baeldung
baeldung.com › home › java › java numbers › how to convert float to int in java
How to Convert float to int in Java | Baeldung
October 17, 2024 - In this example, we can see that ... a float to an int while rounding to the nearest whole number, we can use the Math.round() method....
🌐
Modbuskit
modbuskit.com › en › data-converter
Data Type Converter | IEEE‑754 Float/Hex/Decimal ...
Professional data type conversion tool for IEEE‑754 floating‑point and integer formats: bidirectional Hex ⇄ Decimal conversion covering UINT16/INT16/UINT32/INT32/FLOAT32/UINT64/INT64/FLOAT64, with multiple byte orders (Big Endian/Little Endian/Big Endian – byte swap/Little Endian – byte swap) and real‑time results.
🌐
Study.com
study.com › courses › business courses › business 104: information systems and computer applications
How to Convert Float to Int in Java - Lesson | Study.com
March 12, 2018 - In order to make use of auto-boxing in Java, we first create an instance of the Float class and give it a value. Next, we can make use of the intValue method within the Float class. This generates an integer value.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-float-to-int-in-python
How to convert Float to Int in Python? - GeeksforGeeks
May 10, 2025 - The int() function in Python removes the decimal part and keeps only the whole number. However, due to how floating-point numbers are stored in memory, a value like 5.99999999999999999999 might be treated as 6.0. So, converting it to an integer gives 6.
🌐
Embedded Wizard
doc.embedded-wizard.de › integer-and-floating-point-conversion
Floating-point and integer - Type conversion
The floating-point to integer conversion operator converts the given float operand to a signed or unsigned integer data type. In practice, the conversion is performed in two steps. First the floating-point value is rounded to the next lower integer value.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
How to actually convert float to int? - Programming - Arduino Forum
October 28, 2023 - int(float) returns wildly wrong values... I get that this is some kind of conversion error to be expected, I'm working with a float like 12345.67 and it comes out to weird numbers. ...but I really need to get an int from a float that is correct. I've been all over these forums and the internet ...
🌐
Quora
quora.com › How-do-you-convert-a-float-to-an-integer
How to convert a float to an integer - Quora
Answer (1 of 5): Very carefully! To be more serious about it: a float is isomorphic to a pair \left where i \in \Z is an integer and d \in \left[0, 1\right) is the decimal part. An integer is isomorphic to \left . And this reveals the core challenge in float → integer co...
🌐
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 ...