TEP , The Engineering Projects , Author Welcome! Syed Zain Nasir Mechatronics Engineer TEP , The ENgineering Projects , TEP Badge TEP , The Engineering Projects , Ratting Background 7.5 7.5/10
TEP , The Engineering Projects , Rating Percentage Background Reputation Score 55 / 100
Datatype Conversions in C#, int to float in c#, float to int c#, string to int c#, C# string conversion
TEP , The Engineering Projects , Boxes

Datatype Conversions in C#

TEP , THe Engineering Projects , Shares 2.5K Views
TEP , The ENgineering Projects , Reaction 40
TEP , The ENgineering Projects , Reaction 700
TEP , The ENgineering Projects , Reaction 60
TEP , The ENgineering Projects , Reaction 25
TEP , The ENgineering Projects , Reaction 60
TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest
Shares: 691
TEP , The Engineering Projects , PCBWAY
Datatype Conversions in C#, int to float in c#, float to int c#, string to int c#, C# string conversion
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 C# Data Types 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.
  • 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#, C# string conversion
  • 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#, C# string conversion
  • 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#, C# string conversion
  • 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.Write("\n\nwww.TheEngineeringProjects.com\n");
            string Num1 = "323";
            bool ConversionCheck = int.TryParse(Num1, out int Num2);


            Console.WriteLine("\nValue of Num2 : {0}", Num2);
            Console.WriteLine("\nValue of ConversionCheck : {0}", ConversionCheck);

            if(!ConversionCheck)
            {
                Console.WriteLine("\nString contains non-numeric Values.");
            }
            else
            {
                Console.WriteLine("\nOur Converted int : {0}", Num2);
            }
        }
    }
}
So, that was all about the Datatype Conversions in C#. I hope you have enjoyed today's tutorial. In the next tutorial, we will have a look at How to use Arrays in C#. Till then take care & have fun !!! :)
TEP , The Engineering Projects , Tags
Datatype Conversions in C#
int to float in c#
float to int c#
string to int c#
C# string conversion
TEP , The Engineering Projects , Calender Friday, October 25, 2019
TEP , The Engineering Projects , Comments

Write a Comment

TEP , The Engineering Projects , WordPress TEP , The Engineering Projects , Google TEP , The Engineering Projects , Twitter TEP , The Engineering Projects , Facebook TEP , The Engineering Projects , User
TEP , The Engineering Projects , Robot
TEP , The Engineering Projects , Comments Comments on ‘’ Datatype Conversions in C# ‘’ (0)
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Top PCB Design Service

PCBA
TEP , The Engineering Projects , JLCPCB
PCB
TEP , The Engineering Projects , JLCPCB
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Embedded Tools

ARDINO TEP , The Engineering Projects , JLCPCB
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Subscribe Now !!!

Learn Free Pro Tricks

TEP , The Engineering Projects , Mail Receive Quality Tutorials Straight in your Inbox by Submitting your Email ID Below
TEP , The Engineering Projects , Mail TEP , The Engineering Projects , Mail Shadow
TEP , The Engineering Projects , Mail Robot TEP , The Engineering Projects , Mail Robot Shadow TEP , The Engineering Projects , Mail Robot Shadow
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Engineering Books

TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for arduino $20
TEP , The Engineering Projects , Rating Stars (5.0)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for Raspberry Pi $20
TEP , The Engineering Projects , Rating Stars (3.0)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for arduino $20
TEP , The Engineering Projects , Rating Stars (4.7)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for Raspberry Pi $20
TEP , The Engineering Projects , Rating Stars (5.0)
TEP , The Engineering Projects , TEP Robot TEP , The Engineering Projects , TEP Robot Shadow TEP , The Engineering Projects , TEP Robot Shadow
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Categories

TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K