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
As you can see, the compiler automatically converts the int value 9 to a float value of 9.000000.
Discussions

How to convert int to float in C? - Stack Overflow
I am trying to solve: int total=0, number=0; float percentage=0.0; percentage=(number/total)*100; printf("%.2f", percentage); If the value of the number is 50 and the total is 100, I should get 5... More on stackoverflow.com
🌐 stackoverflow.com
How do I cast my int to float?
To cast a value, you put the destination type in brackets before the value or variable you want to cast, for example (float)x would cast x to a float. However, I assume you're talking about L and S here, in which case just casting won't do you any good since you've declared them as int. They won't be able to hold any decimal places, so casting them to a float would just give you the same wrong number but as a float instead of an int. More on reddit.com
🌐 r/cs50
4
6
October 4, 2021
c - Convert integer to float while also dividing down the scale, without data loss - Game Development Stack Exchange
Stack Exchange network consists ... and build their careers. Visit Stack Exchange ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I'm working on a game, and my intent is to avoid use of floating point for unit ... More on gamedev.stackexchange.com
🌐 gamedev.stackexchange.com
July 29, 2020
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 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
Does implicit conversion always work in int to float in C?
Yes, but only in mixed-type expressions. For example, int + float will convert int to float implicitly.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › int-to-float
How to Convert Int to Float in C? 3 Programs
Can we convert integer to float in C program automatically?
Yes, when an int is used with a float in an expression, the compiler automatically promotes it, helping to convert integer to float in C program without manual casting.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › int-to-float
How to Convert Int to Float in C? 3 Programs
🌐
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 ...
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_type_casting.htm
Type Casting in C
The cast operator causes the division of one integer variable by another to be performed as a floating-point operation
🌐
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!
Find elsewhere
🌐
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);

}

🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › c › FLP06-C.+Convert+integers+to+floating+point+for+floating-point+operations
FLP06-C. Convert integers to floating point for floating-point operations - SEI CERT C Coding Standard - Confluence
In this compliant solution, the decimal error in initialization is eliminated by ensuring that at least one of the operands to the division operation is floating point: void func(void) { short a = 533; int b = 6789; long c = 466438237; float d = a / 7.0f; /* d is 76.14286 */ double e = b / 30.; /* e is 226.3 */ double f = (double)c * 789; /* f is 368019768993.0 */ }
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-language › casting-integers-to-floating-point-values
Casting Integers to Floating-Point Values | Microsoft Learn
August 3, 2021 - For example, casting an unsigned long (with 32 bits of precision) to a float (whose mantissa has 23 bits of precision) rounds the number to the nearest multiple of 256.
🌐
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;
🌐
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.
Top answer
1 of 3
24

I think you're making this too complicated. The trick to preserving precision in your floating point numbers is just to keep their magnitude small. No chaining through doubles or two-stage fractional/whole conversion required.

If you want millimetre accuracy, single-precision floats will keep that as long as your numbers are less than about 16 km.

(If your play space is even smaller than that, then you actually lose precision with your millimetre integer scheme, so you might as well stick with floats throughout)

"But my world is bigger than 16 km" — that's not a problem, because the immediate neighbourhood of your camera, where millimetre-sized errors might actually be visible, is not.

When you compute the matrices to send down the pipe to your GPU, all you need to do is form the translation component as a relative offset from the camera.

Vector3Float translation = (object.integerPosition - camera.integerPosition) / 1000f;

So as far as the GPU is concerned, your camera is always at (0, 0, 0) this way, giving you maximum precision in any direction you look. Objects a very long way from the camera can still suffer precision loss, but a 1 mm offset over a 16 km distance barely changes the direction of the vector at all — your rounding to the pixel grid of the screen is actually a much larger source of error than anything you'd get from floats in this scenario.

The other members of the object transformation matrices will already tend to be near the -1...1 range where floats have very high precision, so you don't have to do anything fancy with them.

This goes in general for other values too. If it's an absolute measure, like a position in the world or a time stamp, use integers so that you have the same precision no matter where/when you are. If it's a relative measure, like an offset/displacement, duration (like deltaTime), velocity, etc. then use a float. This gets you consistent relative precision, where the errors stay small in proportion to the quantity you're measuring.

2 of 3
5

In a comment I mentioned in passing that you could switch to representing 1024ths of meters instead of millimeters. That also helps with representability generally. Your code would look like this:

int i = 1234567890;
float f = ((float)i) / 1024.f;

You don't need to worry about representability at all here. The temporary float value will already be the best representation of i possible in a single precision float, and the divisor 1024.f is not only a perfectly representable floating point number (feel free to write it with the wonky hexadecimal floating literal notation if you prefer) but will actually reduce to a trivial subtraction of the exponent of your floating point value when executed.

Of course you (and your compiler) will probably notice that a division isn't necessary at all at this point. The above code could we written with a similar multiplication (or even maybe some fancy hardware instructions):

int i = 1234567890;
float f = ((float)i) * 0x1p-10f;

(0x1p-10f is just a direct representation of the value 1.f / 1024.f)

🌐
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 ...
🌐
Cplusplus
cplusplus.com › doc › tutorial › typecasting
Type Casting
Here, the value of a is promoted from short to int without the need of any explicit operator. This is known as a standard conversion. Standard conversions affect fundamental data types, and allow the conversions between numerical types (short to int, int to float, double to int...), to or from ...
🌐
Unreal Engine
forums.unrealengine.com › development › programming & scripting
C++: Convert int to float - Programming & Scripting - Epic Developer Community Forums
August 8, 2018 - Hi, I just wanted to know how to convert int to float in c++. The only information about conversions of variables I found was this link. link text But there they only convert FString to another variable.
🌐
Quora
quora.com › How-do-you-convert-an-int-to-a-float-in-C-without-losing-information
How to convert an int to a float in C++ without losing information - Quora
Answer (1 of 4): Your question presupposes something that’s not true. Say an int is 32 bits Say a float is 32 bits ( IEEE single precision.) To faithfully represent the largest integer, you need 31 bits. IEEE floating point will give you 24 bits of mantissa. So it comes up short. What you cou...
🌐
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 - This article dives deep into the various methods of float-to-int conversion, exploring their nuances, potential pitfalls, and practical applications.
Top answer
1 of 5
3

Yes, an integral value can be added to a float value.

The basic math operations (+, -, *, /), when given an operand of type float and int, the int is converted to float first.

So 15.0f + 2 will convert 2 to float (i.e. to 2.0f) and the result is 17.0f.

In your expression (float)15/2 + 15/2, since / has higher precedence than +, the effect will the same as computing ((float)15/2) + (15/2).

(float)15/2 explicitly converts 15 to float and therefore implicitly converts 2 to float, yielding the final result of division as 7.5f.

However, 15/2 does an integer division, so produces the result 7 (there is no implicit conversion to float here).

Since (float)15/2 has been computed as a float, the value 7 is then converted to float before addition. The result will therefore be 14.5f.

Note: floating point types are also characterised by finite precision and rounding error that affects operations. I've ignored that in the above (and it is unlikely to have a notable effect with the particular example anyway).

Note 2: Old versions of C (before the C89/90 standard) actually converted float operands to double in expressions (and therefore had to convert values of type double back to float, when storing the result in a variable of type float). Thankfully the C89/90 standard fixed that.

2 of 5
2

Rule of thumb: When doing an arithmetic calculation between two different built-in types, the "smaller" type will be converted into the "larger" type.

double > float > long long(C99) > long > short > char.

b = (float)15/2 + 15/2;

Here the first part, (float)15/2 is equivalent to 15.0f / 2. Because an operation involving a "larger" type and a "smaller" type will yield a result in the "larger" type, (float)15/2 is 7.500000, or 7.5f.

When it comes to 15/2, since both operands are integers, the operation is done only on integer level. Therefore the decimal point is stripped (from int), and only gives 7 as a result.

So the expression is calculated into

b = 7.5f + 7;

No doubt you'll have 14.500000 as the final result, because it's exactly 14.5f.