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
🌐
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().
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 to convert int to float in C?
To convert int to float in C, you can use type casting like (float)num. It changes the integer value to a float and allows decimal representation.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › int-to-float
How to Convert Int to Float in C? 3 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
🌐
Reddit
reddit.com › r/cs50 › how do i cast my int to float?
r/cs50 on Reddit: How do I cast my int to float?
October 4, 2021 -

int main (void)

{

string text = get_string("Text: ");

int Letters = count_letters(text);

int words = count_words(text);

int sen = count_sen(text);

int L = (100/words) * Letters;

int S = (100/words) * Letters;

int index = 0.0588 * L - 0.296 * S - 15.8;

int index1 = round(index);

printf("%i\n", Letters);

printf("%i\n", words);

printf("%i\n", sen);

printf("%i\n", index1);

}

🌐
WsCube Tech
wscubetech.com › resources › c-programming › programs › int-to-float
How to Convert Int to Float in C? 3 Programs
October 17, 2025 - Learn 3 simple ways to convert an int to a float in C using type casting, implicit conversion, and format specifiers. Includes examples, output, and explanations. Read now!
🌐
Carnegie Mellon University
cs.cmu.edu › ~rbd › papers › cmj-float-to-int.html
Danger in Floating-Point-to-Integer Conversion
Note that I add an extra 0.5 before truncating to simulate rounding behavior: i = (((int) (f + 32768.5)) - 32768) This also produces correct results. These last two algorithms essentially work around the default C conversion semantics, but unfortunately, the conversion itself is slow in most ...
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_type_casting.htm
Type Casting in C
It is considered good programming practice to use the cast operator whenever type conversions are necessary. 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.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › convert-int-to-float
Convert int to float - GeeksforGeeks
March 26, 2024 - ... #include <iostream> using namespace std; int main() { int integerValue = 10; // Converting integer to float explicitly float floatValue = static_cast<float>(integerValue); cout << floatValue <<endl; // Output will be 10.0 return 0; }
🌐
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 …
🌐
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); }
🌐
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.
🌐
Notes
notes.iamdev.in › home › conversion in c: converting float to in
Conversion in C: Converting Float to In - Notes » - iamdev
August 13, 2024 - We declare a float variable d to store the user’s floating-point input and an int variable i to store the converted integer value. ... The printf function displays a message asking the user to enter a floating-point value. The scanf function then reads the input and stores it in variable d. ... This line performs the explicit type conversion. We cast the floating-point value d to an integer type and store the result in i.
🌐
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 ... 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 ......
Top answer
1 of 2
2

13F / 10 cannot be represented precisely with binary encoded float variables.

If you try and print more decimals, you will see what happens:

#include <stdio.h>

int main(void) {
    float current_i;
    float floating_part;
    int float_to_int_part;

    current_i = 13;

    printf("current_i = %.16f\n", current_i);
    current_i = current_i / 10;
    printf("new current_i = %.16f\n", current_i);

    floating_part = (current_i - (int)current_i);
    printf("floating_part = %.16f\n", floating_part);

    float_to_int_part = (int)(floating_part * 10.0);
    printf("float_to_int_part = %d\n", float_to_int_part);

    return 0;
}

Output:

current_i = 13.0000000000000000
new current_i = 1.2999999523162842
floating_part = 0.2999999523162842
float_to_int_part = 2

Note also that float values are converted to double when passed to printf, and the expression (int)(floating_part * 10.0) is evaluated using double arithmetics because 10.0 is a double constant. These values would not be represented precisely as double either, but converting float to double makes the imprecision much more visible and the approximation for the float representation is smaller than the actual value 1.3 whereas with double type, the representation is slightly larger, so the final conversion produces the expected value:

#include <stdio.h>

int main(void) {
    double current_i;
    double floating_part;
    int float_to_int_part;

    current_i = 13;

    printf("current_i = %.23f\n", current_i);
    current_i = current_i / 10;
    printf("new current_i = %.23f \n", current_i);

    floating_part = (current_i - (int)current_i);
    printf("floating_part = %.23f\n", floating_part);

    float_to_int_part = (int)(floating_part * 10.0);
    printf("float_to_int_part = %d\n", float_to_int_part);

    return 0;
}

Output:

current_i = 13.00000000000000000000000
new current_i = 1.30000000000000004440892
floating_part = 0.30000000000000004440892
float_to_int_part = 3

For these computations to be evaluated reliably, you would need to use a decimal based representation for floating point values. The C Standard does not specify such types by default but as an extension described in a Technical Report . Types _Decimal32, _Decimal64 and _Decimal128 might be available on your system (for example gcc supports them on selected targets).

2 of 2
-2

This answer is not a proper solution, and I do not know if one exists. As said below, this is a "band-aid" solution, as double precision does not mean it will fix the issue in all cases (it only happened to 'fix' it in this one)

Try with double instead of float. And see this as bolov stated.

It works here

You also do not need the (int) cast as it is implicit when you declare float_to_int_part as int

🌐
Cprogramming
cboard.cprogramming.com › c-programming › 107408-int-float-conversion.html
int to float conversion
September 25, 2008 - Direct cast does not work in a way I expected. ex: in hex BFFFF2E5 singned int -1073745179 When I cast ... I got -1.073745e+09 Value I expect is: float :-1.999600e+00 (from GHex or other hex editor) How to get this value in C code? Daniel ... Conversion of int to float does not generally do a bitwise copy. It converts the · value put in. -1073754179 is approximately equal to -1.073...e+9. A simple way to get the result you expect (assuming int and float are the same size - not always true)
🌐
W3Schools
w3schools.com › c › c_type_conversion.php
C Data Type Conversion
As you can see, the compiler automatically converts the int value 9 to a float value of 9.000000. This can be risky, as you might lose control over specific values in certain situations. Especially if it was the other way around - the following example automatically converts the float value 9.99 to an int value of 9:
🌐
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
NaN/inf: converting NaN or ±inf to an integer has undefined behavior; guard beforehand. Precision: float has limited precision; large integers may lose precision before casting.
🌐
TechRepublic
techrepublic.com › home › topics › software › web development › convert integer to float in c
Convert Integer to Float in C - TechRepublic
November 14, 2003 - Type casting seems to be the best option for you. You can either declare a float variable and assign the int value to it or use the casting operator to convert the int value for the operation.