pcbway




Datatype Conversions in C#, int to float in c#, float to int c#, string to int c#Hello friends, I hope you all are doing great. In today’s tutorial, we are gonna have a look at Datatype Conversions in C#. We have discussed Datatypes in C# in our 3rd tutorial and today we will discuss How to convert one datatype into another.

If you are working on some complex project, then you have to convert one form of data into another i.e. let’s say you are getting bill value from barcode reader, this value must be a string and if you want to apply any arithmetic operation on it then you need to convert that string into integer or float. So, let’s get started with Datatype Conversions in C#:

Datatype Conversions in C#

  • If you are working on some data driven application, then there’s always a need to convert one form of data into another and that’s where Datatype Conversions are required.
allpcb
  • Few conversions doesn’t require any method or class i.e. if we want to convert an integer into float then that can easily be done by the compiler, as there’s no chance of data loss.
  • Such conversions are called Implicit Conversions, here’s an example:

Datatype Conversions in C#, int to float in c#, float to int c#, string to int c#

  • You can see in above figure that compiler has automatically converted integer into float.
  • But if we want to convert a float (let’s say 313.56) into integer then we can’t do that as integer doesn’t have the decimal part.
  • So, if we want to convert a float into integer, we will get a compiler error i.e. overflow exception.
  • In order to perform such conversions we have to use Explicit Conversions method, pre-defined in C#.
  • There are two options for Explicit Conversions:
    • Use Cast Operator.
    • Use Convert class of C#.
  • Both of these methods are shown in below figure:

Datatype Conversions in C#, int to float in c#, float to int c#, string to int c#




  • Now you can see in above figure that the Cast Operator (int) has just taken the main value and simply ignored the decimal/fractional part.’
  • While the Convert Class (Convert.ToInt32) has rounded off the number and didn’t ignore the decimal part.

String Conversion in C#

  • Normally the data received from external/hardware devices, is in the form of strings and we need to convert it into integer or float etc to perform arithmetic operations.
  • There are two options available for String Conversions in c#, which are:
    • Parse.
    • TryParse.
  • Here’s the implementation of Parse Method for String to integer conversion, in below figure:

Datatype Conversions in C#, int to float in c#, float to int c#, string to int c#

  • But there’s a drawback in Parse that if the string contains alphabetical/special characters then it will create an error and couldn’t convert the string into integer.
  • So, in order to avoid error, we should use TryParse method, as shown in below figure:

Datatype Conversions in C#, int to float in c#, float to int c#, string to int c#



  • As you can see in above figure, TryParse takes two parameters:
    • First one must be a string, which you want to convert.
    • Second one must be an integer, in which the value will be saved.
  • Moreover, TryParse method also returns a Boolean type, which will be:
    • True, if the conversion is successful.
    • False, if the conversion is unsuccessful.
  • I have provided a string “323ABC” whcih contains alphabetical characters, that’s why ConversionCheck is false and we are not getting any value in our integer.
  • Here’s a screenshot of one successful conversion of TryParse method:

Datatype Conversions in C#, int to float in c#, float to int c#, string to int c#

  • Now TryParse is returning True as the conversion is successful and our output integer variable got updated.
  • Here’s the code used in today’s tutorial:
using System;

namespace TEPProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Code Designed by www.TheEngineeringProjects.com \n");

            string Num1 = "323";

            bool ConversionCheck = int.TryParse(Num1, out int Num2);

            Console.WriteLine("Value is : {0} \n", Num2);
            Console.WriteLine("Value is : {0} \n", ConversionCheck);

        }
    }
}

So, that was all about the Datatype Conversions in C#. I hope you have enjoyed today’s tutorial, will share the next one soon.

Leave a Reply