Converter – converting text to a number
In this article we are going to build a converter, which converts text into a whole umber. As explained in earlier articles text and numbers are saved in different data types. We can simply use “25” as a number as it is a String. But there is a very simple way to convert a number within a String to an actual integer number in CSharp.
Converter – Text to Number in CSharp
In some cases you need to have a certain value in a specific data type, then it is important to being able to convert it. E.g. you have a number within a String variable when scraping it from a text file on your pc and you want to use it for e.g. multiplication, then you can do it the following way:
String numbersString = "25"; int value1 = Int32.Parse(numbersString);
Now value1 contains the value 25 and can be used as a normal integer in further code.
What if you need it the other way around?
Integer to String convertion – Example for typecasting in CSharp
String anotherNumbersString = String.valueOf(value1);
Now that new variable anotherNumbersString contains the value “25” which is a String and can now be used as a usual text variable.