DEV Community

Tania
Tania

Posted on • Originally published at kodlogs.com

C# convert float to int

Table of contents
Convert float value to int
Example float to int
3 method of conversion
Syntax
Summary
Article Tags

Convert float value to int

When we start programming we are learning a lot of new and important things that we did not know about. One of them is type conversion. First of all you should know that type in any programming language. What is conversion? It is very simple to convert any data type to another data type. I am telling you in a very simple way. Byte b = 20 and float f = b; If we understand carefully, the value that was in b will go to float. We have two types of data types to do conversion in programming. Conversion is what the compiler of c # does by itself. If we have a float. We convert the data type to an integer.

Example float to int:

using System;

namespace convert_float_to_int
{
    class Program
    {
        static void Main(string[] args)
        {
            float f = 10.2f;
            int i = (int)f;
            Console.WriteLine("Converted float {0} to int {1}", f, i);
        }
    }
}

            ( OUTPUT )
Converted float 10.2  to int  10
Enter fullscreen mode Exit fullscreen mode

Alt Text

Example

int myfan = myFloatfan as int;
int myfan= (int) myFloatfan;
Then the compiler automatically performs the conversion. To convert the float to an integer, we write int in brackets, then the value after the point is deleted. And we can also apply the convert class. There are three ways to convert C #.
3 methods of conversion:

1.Implicit type conversion
2.Explicit type conversion
3.Conversion between non compatible type

Alt Text

Example

float myFloat = 3.5F;

int myInt = Convert.toInt32(myFloat);
Enter fullscreen mode Exit fullscreen mode

Syntax

int Convert.ToInt32(float value);

Summary

Today we are understand to kodlog website, I try to explain C# convert float to int . I hope after reading this article you will be able to understand conversion float to int in c#. I would like to have feedback from my kodlog site. Please post your feedback, question, or comments about this article.

Article Tags
C#|Python |Python 3|pycharm |html|java|JavaScript |cpp|code|rubi|Network | Google |html5 |php|bootstrap |string |Microsoft |variable |syntax |Coders |Codinglove|
Computerscience|wordpress|html-page|php|
Xml|game| net| list | loop | integer |float | string | double |

Top comments (0)