This tutorial explains how to convert variable types in the C# language (eg. convert from string to integer or float, or convert from integer or float to string). Here is a summary of the methods used:
- float.Parse() – converts to float type
- int.Parse() – converts to int type
- .ToString() – converts to string type
Watch the video below and then scroll down to see the sample code.
Example of converting from string to float:
float myFloatVariable = float.Parse(myStringVariable);
Example of converting from string to int:
int myIntVariable = int.Parse(myStringVariable);
Example of converting from int or float to string:
string myStringVariable = myFloatVariable.ToString();
Here is the full sample code from the tutorial video:
public static void Main(string args)
Console.WriteLine("Enter your name: ");
string username = Console.ReadLine(); // store user input as string
Console.WriteLine("Hello there, " + username);
Console.WriteLine("Enter two numbers");
float num1 = float.Parse(Console.ReadLine()); // convert user input from string to float
float num2 = float.Parse(Console.ReadLine()); // convert user input from string to float
Console.WriteLine("The result is: " + (num1 + num2)); // display string of text and float result
int num3 = 5;
int num4 = 10;
Console.WriteLine(num3.ToString() + num4.ToString()); // convert int to string
Next tutorial: Making comparisons in C#
In the previous tutorial we looked at how to create variables and work with the string data type in C#. In this tutorial we will look at how to work with the integer and float data types in C# and also how to convert strings to integers or floats using the int.Parse() and float.Parse() methods. You will also learn how to manipulate numbers using arithmetic operators.
Watch the video below or scroll down to see the sample code.
Sample C# code
public static void Main (string args)
Console.WriteLine ("Enter two numbers...");
float num1 = float.Parse(Console.ReadLine ());
float num2 = float.Parse(Console.ReadLine ());
Console.WriteLine (num1 + num2);
Arithmetic operators are used for performing standard math operations on variables and are usually only used number variables (although they can be used for other things too).
||Name / description
||Addition – this operator is used to add two numbers together. It can also be used to concatenate (join) two strings together.
||Subtraction – this operator is used to subtract one number from another.
||Multiplication – this operator is used to multiply two numbers together.
||Division – this operator is used to divide one number by another.
||Modulus – this operator is used to divide one number by another but instead of returning the result, it returns the remainder of the division. Eg. 5%2 would return a result of 1.
Brackets can also be used for more complex math operations eg. 5 + (10 * (6 / 3) / 2);
The assignment operators follow standard mathematic order of operations. That means that the math works from left to right. Parenthesis are done first, multiplication and division comes second, and then addition and subtraction come third.
Assignment operators are used to assign a value to a variable. The most frequently used assignment operator is the equals (=) sign. There are other operators as well that are used to combine multiple operations into one. The syntax of a standard variable assignment looks like this:
<variable name> <assignment operator> <value>;
For example: x = 5;
The table below shows the different assignment operators available in C#.
||The equals sign is used to assign the value on the right side of the equals sign to the variable on the left side of the equals sign.
|+= , -= , *= and /=
||These assignment operators are also used to perform arithmetic operations and assign the result to the variable eg. x *= 5 is the same as saying x = x * 5.
|++ and —
||These assignment operators are called increment and decrement operators and are used to increase or decrease the value of a variable by 5. For example, x++ is the same as saying x = x + 1.
Next tutorial: Conditional programming in C#
This tutorial explains how to convert a bitmap image into a line drawing (or how to trace an image) using Adobe Illustrator. There is no video available for this tutorial, but you can download the PDF instructions here.
The video below explains how to convert between binary and decimal number systems.
This video explains how to easily convert a decimal number into binary, and how to also convert a binary number into decimal.
This video tutorial explains how to concatenate (join) strings, convert between integers and strings, change text to uppercase and lowercase, and also introduces indexing and arrays.
The sample code snippet below shows how to join (concatenate) strings, find the length of a string (the number of characters in a string), convert from integer to string, display specific characters in a string, split a string up and place each word in a separate array element, and convert string to uppercase and lowercase.
Don’t try all of this in one go. Have a go at one string operation at a time and only move on to the next one when you feel comfortable to.
var firstName = "Nicolas";
var lastName = "Cage";
// Concatenation means joining (eg. joining two strings together to form one string
// Below, is an example of a concatenated string being held in a variable
var joinednames = firstName + lastName;
// Below, is an example of concatention with spaces added between words.
document.write(firstName,' ',lastName,' ',"says \"hello\".");
var fullName = firstName + ' ' + lastName; // this combines two variables and a space between the strings into a new variable
var nameLength = fullName.length; // this calculates the length of the string as an integer
var age = 20;
document.write(fullName + age); // this prints the name string and then the variable (no spaces)
var ageString = age.toString(); // this converts the age integer variable to a string variable
/* To convert a string to an integer, the variable must firstly contain ony numbers. It can simply be multiplied by 1 to convert to an integer
eg. newIntVariable = stringVariable * 1
or you can use parseInt and parseFloat functions eg parseInt('77');
var message = 'Hello world!';
document.write(message.indexOf('w')) // the indexOf function finds the index number of a character in a string
document.write(message.charAt(8)) // this gives the character at index number 8
document.write(message.length) // this will give the length of the string
var messageArray = new Array();
messageArray = message.split(' '); //this splits the words (where there is a space) into a new array
document.write(messageArray,"</br>"); // this would return "Hello" (and a new line)
document.write(messageArray); // this would return "world!"
document.write(message.substring(4,8)); //this will show anything between index number 4 and 8
document.write(message.substr(4,8)); //substr is different to substring. this will show the 8 characters after index number 4.
document.write(message.toUpperCase()); // converts string to uppercase
document.write(message.toLowerCase()); // converts string to lowercase
document.write("<b>",message.toUpperCase(),"</b>"); // this prints the uppercase string in bold using the HTML bold element <b>
document.write("<h1>This is a heading</h1>"); // HTML elements can be put straight into a string
Next tutorial: Events