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
🌐
W3Schools
w3schools.com › c › c_type_conversion.php
C Data Type Conversion
Sometimes, you have to convert the value of one data type to another type. This is known as type conversion. For example, if you try to divide two integers, 5 by 2, you would expect the result to be 2.5. But since we are working with integers (and not floating-point values), the following example ...
Discussions

Does C allow implicit casting/conversion from float to an int?
Yes, this sort of conversion is implicitly performed. Behaviour is undefined if the result doesn't fit. More on reddit.com
🌐 r/C_Programming
36
28
November 21, 2020
Converting float to int can be undefined behavior and how to fix it
Usually, when I need to convert from float to int is because I want either truncate or approximate a real number, and in my use case this approximation is mathematically justified. For these reasons I use the functions listed here under the "Nearest integer floating point operation" section What happens if I use such functions to round a float that cannot fit an int? Is this the same type of UB? Edit: typos More on reddit.com
🌐 r/cpp
36
53
December 31, 2021
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
7
0
June 10, 2024
Are int to float conversions slow?
If it really matters, you should benchmark it. If you're not willing to benchmark it, then it clearly doesn't really matter. More on reddit.com
🌐 r/C_Programming
40
32
January 2, 2022
People also ask

How to convert float to int in C?
You can use type casting like (int)num to convert a float value to an integer. It simply drops the decimal part.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › float-to-int
How to Convert Float to Int in C? 4 Programs
How can I safely convert float to int in C?
Use rounding functions if precision matters. For general use, type casting like (int)num is enough.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › float-to-int
How to Convert Float to Int in C? 4 Programs
Is math.h required for float to int conversion?
Not for type casting, but required if you use round(), floor(), or ceil() for rounding before conversion.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › float-to-int
How to Convert Float to Int in C? 4 Programs
🌐
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 fro…
🌐
Thiyagaraaj
c-lang.thiyagaraaj.com › archive › c-blog › convert-a-floating-point-value-to-an-integer-in-c
Convert a Floating-point value to an Integer in C - C Programming
void main() { // Declaring Variables float x=1.9,y=2.4; int a,b; printf("Value Of x:%f",x); printf("Value Of y:%f",y); // Simple Conversion a = (int)(x+0.5); b = (int)(y+0.5); printf("Value Of a:%d",a); printf("Value Of b:%d",b); }
🌐
WsCube Tech
wscubetech.com › resources › c-programming › programs › float-to-int
How to Convert Float to Int in C? 4 Programs
October 17, 2025 - Learn how to convert a float to an int in C with 4 simple programs. Explore different methods using type casting, floor(), ceil(), and round().
Find elsewhere
🌐
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 ...
🌐
Medium
medium.com › @ryan_forrester_ › c-float-to-int-conversion-how-to-guide-aea5be6d3d4b
C++ Float to Int Conversion (How To Guide) | by ryan | Medium
January 7, 2025 - When working with financial calculations, using floats can lead to rounding errors. Consider this seemingly simple calculation: float price = 19.99f; int cents = static_cast<int>(price * 100); // Expect 1999, but might get 1998!
🌐
Jukka Korpela
jkorpela.fi › round.html
How can I convert a floating-point value to an integer in C?
For positive floating-point values, the simplest way to achieve a rounding conversion is to use an expression like · (long) (x+0.5) but it is better to be prepared for negative values, even if you do not expect them to occur. This means that one should use the conditional expression · x >= 0 ? (long)(x+0.5) : (long)(x-0.5) The value of this is expression is the integer which is nearest to the value of the floating-point value of x.
🌐
Carnegie Mellon University
cs.cmu.edu › ~rbd › papers › cmj-float-to-int.html
Danger in Floating-Point-to-Integer Conversion
float f; /* assume -32768 <= f <= 32767 */ int i; /* could also be “shortint” */ i = (int) f; /* “(int) f” means “convert f to an int” */ The default float-to-integer conversion in C does not round to the nearest integer, but instead truncates toward zero.
🌐
Medium
medium.com › @brajagopal.tripathi › how-to-convert-any-float-number-to-an-integer-in-c-a6cc59e5f9f5
How to convert any float number to an integer in c? | by Brajagopal Tripathi | Medium
September 20, 2023 - How to convert any float number to an integer in c? There are two ways to convert a float number to an integer in C: Using the built-in int() function. This function will take a float as an input and …
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_type_casting.htm
Type Casting in C
While performing the implicit or automatic type conversions, the C compiler follows the rules of type promotions. Generally, the principle followed is as follows − · Byte and short values − They are promoted to int. If one operand is a long − The entire expression is promoted to long. If one operand is a float − The entire expression is promoted to float.
🌐
Lindevs
lindevs.com › convert-float-to-integer-representation-using-c
Convert Float to Integer Representation using C | Lindevs
June 30, 2025 - #include <stdio.h> unsigned int asuint(const float f) { const union { float f; unsigned int i; } u = {f}; return u.i; } unsigned long long asuint64(const double f) { const union { double f; unsigned long long i; } u = {f}; return u.i; } int main() { printf("%u\n", asuint(1.0f)); // 1065353216 printf("%llu\n", asuint64(1.0)); // 4607182418800017408 return 0; } ... In low-level programming, especially when dealing with binary data formats or debugging floating-point precision issues... ... In C, memory alignment can be a critical concern - especially when you're working with... ... When writing or maintaining C code, it's often helpful to know which version of the...
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › convert-int-to-float
Convert int to float - GeeksforGeeks
March 26, 2024 - Converting an integer to a floating-point number involves changing the data type of the variable from int to float, allowing decimal values. This conversion is useful when performing arithmetic operations that result in fractional values or when working with numerical data that requires precision ...
🌐
STMicroelectronics Community
community.st.com › t5 › stm32cubeide-mcus › conversion-of-int-to-float-and-float-to-int › td-p › 632723
Solved: Conversion of int to float and float to int - STMicroelectronics Community
January 26, 2024 - There's no need to complicate this with typedefs. int16_t x = 42; float x2 = (float) x; float y = 42.0f; int16_t y2 = (int16_t) y;
🌐
Quora
quora.com › How-do-you-convert-a-float-to-an-integer-using-casting-in-the-programming-language-C
How to convert a float to an integer using casting in the programming language C++ - Quora
Precision: float has limited precision; large integers may lose precision before casting. ... int i = static_castwzxhzdk:6(std::lround(f)); // requires wzxhzdk:7, rounds to nearest with ties to even depending on function
🌐
Notes
notes.iamdev.in › home › conversion in c: converting float to in
Conversion in C: Converting Float to In - Notes » - iamdev
August 13, 2024 - This operation truncates the decimal part of the floating-point number, keeping only the integer part. ... Returning 0 indicates that the program executed successfully. ... Write the Code: Save the code in a file named float_to_int.c.
🌐
Keil
keil.com › support › docs › 2340.htm
GENERAL: Converting Floating-point Number to Integer
Information in this article applies to: C166 All Versions C251 All Versions C51 All Versions QUESTION: I would like to convert a floating-point number to an integer. Is there a standard math library routine to accomplish this? ANSWER: No. The C Programming Language does not require library...
🌐
Reddit
reddit.com › r/cpp › converting float to int can be undefined behavior and how to fix it
r/cpp on Reddit: Converting float to int can be undefined behavior and how to fix it
December 31, 2021 -

Have you ever written code like this?

void foo(float f) {
    int i0 = f;
    int i1 = int(f);
    int i2 = static_cast<int>(f);
}

All of these conversions are undefined behavior if the float is out of bounds for the int. [0]

This is surprising, easy to miss and badly designed as there is no standard way of safely performing this conversion. In practice you will get an unspecified int because compilers emit the corresponding converting assembly instruction but UB is still bad and should be avoided.

I wrote a small library to fix this https://github.com/e00E/cpp-clamp-cast .

With it the previous unsafe code can be turned into the safe

void foo(float f) {
    int i = clamp_cast<int>(f);
}

[0] https://en.cppreference.com/w/cpp/language/implicit_conversion section "Floating–integral conversions":