Convert Float to Int in C#
This tutorial will introduce methods to convert a float value to an integer value in C#.
Convert Float to Int With Explicit Typecasting in C#
Typecasting is a way of converting a value from one data type to another data type. The Float data type takes more bytes than the Int data type. So, we have to use explicit typecasting to convert the float value to an Int value. The following code example shows us how to convert a float value to an integer value with explicit typecasting in C#.
Please enable JavaScript
We converted the float variable f to the integer variable i with explicit typecasting in C#. The (int) is used to cast f to i . The problem with this approach is that it ignores all the values after the decimal point. For example, the float value 10.9 is also converted to the integer value 10 .
Convert Float to Int With Math.Ceiling() Function in C#
If we want to consider the values after the decimal point, we have to use some approach other than explicit typecasting. The Math.Ceiling() function converts a decimal value to the next integer value. The Math.Ceiling() function returns a double value which can be converted to an integer value using explicit typecasting. The following code example shows us how we can convert a float value to an integer value with the Math.Ceiling() function in C#.
We converted the float variable f to the integer variable i with Math.Ceiling() function in C#. The (int) is used to cast the double value returned by the Math.Ceiling() function to an integer value. The problem with this approach is that it always returns the next integer value. For example, the float value 10.1 is also converted to the integer value 11 .
Convert Float to Int With Math.Floor() Function in C#
The Math.Floor() function can also be used along with explicit typecasting to convert a float value to an integer value in C#. The Math.Floor() function is used to convert the decimal value to the previous integer value. The Math.Floor() function returns a double value that can be converted to an integer value with explicit typecasting. The following code example shows us how we can convert a float value to an integer value with the Math.Floor() function in C#.
We converted the float variable f to the integer variable i with the Math.Floor() function in C#. The (int) is used to cast the double value returned by the Math.Floor() function to an integer value. The problem with this approach is that it always returns the previous integer value. For example, the float value 10.9 is also converted to the integer value 10 .
Convert Float to Int With Math.Round() Function in C#
The approaches discussed above do work, but there is some drawback of every approach. If we want to convert a float value to an integer value but do not want our data to change, we can use the Math.Round() function in C#. The Math.Round() function, as the name suggests, is used to round up decimal values to the closest integer value. The Math.Round() returns a double value, which can be converted to the integer data type with explicit typecasting. The following code example shows us how we can convert a float value to an integer value with the Math.Round() function in C#.
We converted the float variable f to the integer variable i with the Math.Round() function in C#. The (int) is used to cast the double value returned by the Math.Round() function to an integer value. This is the optimal approach for converting a float value to an integer value in C#.
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
Sidebar
Latest
Converting float to int in C
In this post, we’ll see how to convert a float to an int in C. We’ll assume both int and float datatypes to be 32-bits long an int using two’s complement representation for negative values. The conversion will be performed using the bit-level representation of the float data type. Only addition, subtraction, and bitwise operations will be used.
To convert from int to float , you can see this post.
This is also a solution for exercise 2.96 of the Computer Systems: A Programmer’s Perspective book.
Rounding to whole numbers
Before we begin, let’s study how C will handle rounding from float to int . The rounding mode used is round towards zero.
For positive values, we round down:

Negative values are rounded up:

In practical terms, the fractionary part is truncated.
Getting the floating-point fields
The floating-point single-precision data representation ( float ), consists of three different fields.
source
These fields are used to encode a number in the form:

- One bit encodes the sign: s .
- 8 bits encode the exponent, E. We’ll call this bits exp (in green on the image above).
- 23 bits encode the magnificand, M. We’ll call them frac bits (in red on the image above).
For our converter, we’ll start by getting these different fields.
They are obtained by shifting operations combined with masks to get the necessary bits. On the code snippet above, f contains the 32-bit float representation of the value.
Special cases: NaN and Infinity
Floating-point encodes special values such as NaN (not a number, obtained by expressions such as sqrt(-1) or 0.0 / 0.0 ) and infinity (for values that overflow) with the exp field all set to ones. For these special cases, we’ll return the special value 0x80000000 (which corresponds to INT_MIN ).
A value preceded by 0x , such as 0xFF , is used to denote an hexadecimal number.
Denormalized values
Denormalized values are those in which the exp field is all zeros. In floating-point representation, they are used to encode values that are very close to zero (both positive and negative). All of these values will be rounded to zero.
Normalized values
Finally, we can focus on normalized values. In this case, the exp field is neither all ones nor all zeros. For this group of values, the exponent E is encoded as:

Where exp is the unsigned 8-bit number represented by the exp field and Bias = 127. We can get the exponent E as follows:
Values less than one
First, let’s consider values that are in the range -1 to 1 (not including -1 and 1). All of these values will be rounded to zero. Positive values will be rounded down, and negative values will be rounded up. In both cases, the result will be 0. These cases will take place when the exponent E is less than 0:
For instance, if E = -1 , the encoded float will be a binary fraction in the form:

Where XXX represents the fractionary part of the magnificand, encoded by the frac field. 0.1XXX is a binary fraction less than one.
Overflowing
The range of int values is between INT_MIN = -2^31 = 2147483648 and INT_MAX = 2^31 — 1 = 2147483647 . For float values beyond that range, we’ll return 0x80000000 as a way of encoding overflow.
The int datatype represents numbers using 31 bits (the remaining bit is used for the sign). This means that the binary representation of our value can be at most 31 bits long. This limit case will take place when E = 30 :

X. X are the 23 bits of the frac field. The value 1X. X0. 0 will be 31 bits long: a leading one + 23 frac bits + 7 zeros.
We can conclude that float will overflow when E > 30 :
Normalized values in the range of int
Finally, we’re left with the float values that can be rounded to an int other than zero and that won’t overflow.
The frac field is 23 bits long. Remember that the exponent E and frac encode a value in the form:

Where each X is one of the 23 binary digits of the frac field. They represent the digits that come after the binary point (fractionary part).
A positive value of E will shift the binary point E places to the right. For instance if E = 2 :

Here we’re using the symbol Y for the 21 frac binary digits that come after the binary point.
As it was mentioned above, when we cast a float to int , C will truncate the fractionary part.

That means that the rightmost 23 — 2 = 21 bits of the frac field are discarded.
On the other hand, if E is larger than 23, the binary point will be shifted beyond the bits of the frac field and we’ll have extra zeros to the right:

We will have E — 23 trailing zeros.
These conditions can be fulfilled by the appropriate shifting operations. We shift frac to the right ( >> ) in order to discard the least significant bits when E < 23 , and we shift to the left ( << ) to add trailing zeros.
Here x is the resulting bit-level int representation. First, we shift the leading one E places to the left. Then, we get the integer part of the float value by shifting operations as it was mentioned above. Note that we use the bitwise OR operator ( | ) as a way of “adding” the leading one and the lower order bits of the frac field (e.g. 100 | 001 = 101 ).
Negative values
Finally, we modify the bit encoding for negative values.
x+1 is just a binary operation that yields -x in two’s complement representation.
Complete program and testing
Putting all of the pieces together in the float_f2i function, we get:
In order to handle the bit-level representation of float values with bitwise operators, we use the unsigned datatype which we call float_bits . Additionally, we wrote some of the numerical constants as K = 8 for the length of the exp field and N = 23 for the length of the exp field.
Testing
The program was tested as follows:
We used the variable bits to generate all the possible bit-level combinations. The address of bits was then referenced as datatypes float and float_bits to perform the test. Finally, we verified that the result of casting float to int was the same as the return value of the float_f2i function.
When compiled, we received no error output which means that the converter worked for all of the tested values.
How to convert float to int with Java
I used the following line to convert float to int, but it’s not as accurate as I’d like:
The result is : 8 (It should be 9 )
When a = -7.65f , the result is : -7 (It should be -8 )
What’s the best way to do it ?
![]()
9 Answers 9
Using Math.round() will round the float to the nearest integer.
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)
note: this is, by contract, equal to (int) Math.floor(f + 0.5f)
truncate (i.e. drop everything after the decimal dot)
ceil/floor (an integer always bigger/smaller than a given value if it has any fractional part)
Python float to int

The following article provides an outline for Python float to int. In Python, all the values we use or store as a variable will have a unique data type. It explains about the nature of the value, and depending on that, Python automatically allocates a data type for that value, and it helps in defining the kind of operation that is allowed to be done and stored in a particular structure bypass it to the compiler, and the Data types are mutable and for specific operations. Users can change the data types from float to int or string and vice-versa.
Syntax of Python float to int
Web development, programming languages, Software testing & others
Python has different data types for a different set of values, Integers deals with numbers, and float deals with both decimal and numeric characters, Boolean deals with Binary values (True or False), and there are strings that could take alphanumeric values, and python allows different data structures like List, Tuple, Dictionary & Sets for working with different problems.
In the below example, we have declared 4 different values and checked the data types of the values to give an idea of the python data types.
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
Code:
Output:

How to Convert float to int in Python?
Let us see the two functions used in the float to int conversion and vice versa.
1. float() Function
This function returns a floating-point value from a string or a number.
Syntax:
Only one parameter can be used in this method and is optional. There are two types of argument that can be used in this function.
- Number: The number can be any floating-point number or an integer.
- String: A string must contain numbers of any kind.
This method ignores any right or left whitespaces. NaN or int can be used. We can use mathematical operators as well.
2. int() Function
The int() function converts the given value to an integer value.
Syntax:
If no arguments are passed in the function, it returns zero. The default base is set as 10 if no base value is given. An integer object can be constructed using a number or a string.
Examples of Python float to int
Given below are the examples of Python float to int:
Example #1
Let us see a simple example to print the integer value of a given variable.
Code:
Output:

In the above code, we have declared the value of 5 to the variable called a.
In the next line, we have used the int() function to obtain the integer value of 5 and printed the same.
Example #2
Code:
Output:

In the above code, we have declared the value as 10/3 to the variable x.
In the next line, we have printed the value of x, which is a floating-point number.
In the next line, we have converted the value to an integer value using the int() function and printed the integer value.
Example #3
In the next example, we will see how to add two float values and convert them into an integer value.
Code:
Output:

In the above example, we have assigned two values of 5.3 and 3.2 to a and b, respectively. In the next line, we have added both a and b assigned the variable to another variable c. In the next line, we have printed the float value of c using the float() function. Finally, we have converted the float value to the integer value in the next line and printed the same.
Example #4
In this example, we will see how to subtract two float values and convert the result to an integer value.
Code:
Output:

In the above code, two float values of 10.1 and 5.5 are assigned to two variables, x and y respectively. Then the subtracted value is stored in the z variable. In the next line float value of z is printed, and in the last line, the integer value of z is printed.
Example #5
Consider the number 1.82e18. It is another form of a floating-point number. The letter e represents the base10. Thus, the number 1.82e18 can be expressed as 1.82 * 10^18. Let us see a simple code using this form of a floating-point number.
Code:
Output:

In the above code, two floating-point numbers, 1.82e8 & 2e10, are assigned to two variables, a and b respectively. The next two lines just print the value of a and b. In the next two lines, float values of a and b are printed by the float() function. Finally, the last two lines print the integer values of a and b, respectively.
Example #6
In this example, we will see how to do a simple arithmetic operation on floating-point numbers.
Code:
Output:

As done in the previous example, two floating-point numbers 5.82e18 & 2.5e12, are assigned to two variables, a and b, respectively. The next two lines print just the value of a and b. In the next two lines, float values of a and b are printed by the float() function. The two float values are added and stored in the third variable c. The last line prints the integer value of c using the int() function.
Conclusion
In this article, we have seen float to int conversion using various examples. The data type conversion is considered to be very important in python since it is used in various real-time scenarios. Moreover, when working with different kinds of algorithms in the data science field, we need to convert data types.
Recommended Articles
This is a guide to Python float to int. Here we discuss the introduction, how to convert float to int in Python? and examples, respectively. You may also have a look at the following articles to learn more –