Integer division truncates, so (50/100) results in 0. You can cast to float (better double) or multiply with 100.0 (for double precision, 100.0f for float precision) first,

double percentage;
// ...
percentage = 100.0*number/total;
// percentage = (double)number/total * 100;

or

float percentage;
// ...
percentage = (float)number/total * 100;
// percentage = 100.0f*number/total;

Since floating point arithmetic is not associative, the results of 100.0*number/total and (double)number/total * 100 may be slightly different (the same holds for float), but it's extremely unlikely to influence the first two places after the decimal point, so it probably doesn't matter which way you choose.

Answer from Daniel Fischer on Stack Overflow
🌐
WsCube Tech
wscubetech.com › resources › c-programming › programs › int-to-float
How to Convert Int to Float in C? 5 Programs
May 9, 2026 - Learn 5 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!
Discussions

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
How do you convert an int to float so that you can take math ...
🌐 forum.nim-lang.org
CCS :: View topic - int to float conv.
Profile Log in to check your private messages Log in · CCS does not monitor this forum on a regular basis More on ccsinfo.com
🌐 ccsinfo.com
March 13, 2018
Converting int to float in C function - Stack Overflow
So I have some source code that needs to be modified to include a 3rd mathematical formula. The source code is: #include int sum_and_diff (int a, int b, int *res) { int sum; ... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

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? 5 Programs
How does arithmetic conversion help in int to float conversion?
When an integer is used in an expression with a float value (like multiplying by 1.0), it automatically converts to a float during calculation.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › int-to-float
How to Convert Int to Float in C? 5 Programs
Can you use functions to perform int to float conversion?
Yes, you can create a function that takes an integer as input and returns its float equivalent for better code organization.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › programs › int-to-float
How to Convert Int to Float in C? 5 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 ...
🌐
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);

}

🌐
Quora
quora.com › How-do-you-convert-an-integer-to-a-float-without-losing-its-value-in-C-C-programming
How to convert an integer to a float without losing its value in C/C++ programming - Quora
Answer (1 of 2): Integers and floats are coded differently and have different ranges. They both occupy 4 bytes but the ranges do not match. The floats hold 23+1 bits for the mantissa, so they are not able to represent integers larger than 16777216 without a loss of accuracy. https://en.wikipedi...
Find elsewhere
🌐
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 | CERT Secure Coding
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 */ }
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 107408-int-float-conversion.html
int to float conversion
September 25, 2008 - -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) ... There are alternatives involving pointer abuse, but those things are dangerous in hands of children. ... You are telling the compiler to take the number you have (large integer) and make it into a float, not to interpret the number AS float.
🌐
CCS, Inc.
ccsinfo.com › forum › viewtopic.php
CCS :: View topic - int to float conv.
March 13, 2018 - Profile Log in to check your private messages Log in · CCS does not monitor this forum on a regular basis
🌐
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...
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)

🌐
Emory
cs.emory.edu › ~cheung › Courses › 255 › Syllabus › 5-repr › IntFloatConv.html
Converting between integer and float data representations
convert between: But the · int <--> float conversion procedure is · much more complex. The conversion algorithms use · bit manipulation operations that you have · not had before. So I will · not give the complete algorithms, but describe the algorithms only at a high level (mainly to sastify ...
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_type_casting.htm
Type Casting in C
Here, it is simple to understand that first c gets converted to an integer, but as the final value is double, usual arithmetic conversion applies and the compiler converts i and c into 'float' and adds them yielding a 'float' result.
🌐
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.
🌐
W3Schools
w3schools.com › c › c_type_conversion.php
C Data Type Conversion
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A ... 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 will just output 2:
🌐
Arm Learning
learn.arm.com › learning-paths › cross-platform › integer-vs-floats › integer-float-conversions
Learn about integer and floating-point conversions: Integer and floating-point conversions
When an integer is converted to a floating point number, or vice-versa, there is always a conversion instruction involved. And it’s almost always more costly than a mere copy. ... In the next section you will learn more about the potential issues in floating-point conversions. Copyright (c) 2010, The WebM Project authors.
🌐
Carnegie Mellon University
cs.cmu.edu › ~rbd › papers › cmj-float-to-int.html
Danger in Floating-Point-to-Integer Conversion
i = (int)(f + 0.5); C does not round negative numbers down, so values in the interval (-1.5, 0.5) are converted to zero. In contrast, a correct conversion should map only the interval (-0.5, 0.5) to zero. There are several ways to perform rounding for audio, and, surprisingly, proper rounding ...