Cast string to float type in c#

In this article I am going to explain about how to convert string type into float type in c#.
  • 2447

Introduction

In this program we will learn why we use parse method , we use the parse method for casting, we use parse method when we have to make conversion (casting) from one data type to another.

Example can we convert  string  variable into float , all data types have there own range so float can not convert into int implicitly so we will have to need for explicit (casting)

Step 1

First of all we will have to open the new project  in console application.

Step 2

Then we have to take two string variable

        string st = "23.5";

        string str = "24.5";

Step 3        

Then cast them into float by the parse method,  float.Parse

 Example

using System;
using
System.Collections.Generic;
using
System.Linq;

using System.Text; 

namespace ConsoleApplication5
{
    class
Program

    {
        static void Main(string[] args)

        {
            string st = "23.5";

            string str = "24.5";

            float ft = float.Parse(st);

            float flt = float.Parse(str);

            Console.WriteLine(ft + flt);

            Console.ReadLine();

        }

    }

}

Output

nk.jpg

© 2013 dotNetheaven. All rights reserved.