C# Number Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
In C#, you can parse a string with a decimal point to a double using the Double.Parse
method, and you can parse a string with a decimal point to an integer using the Int32.Parse
method. You can also convert a string to an integer using the Int32.Parse
method, convert a hexadecimal string to an integer using the Convert.ToInt32
method, convert a List<string>
to a List<int>
using the List.ConvertAll
method, and convert a string[]
to an int[]
using the Array.ConvertAll
method. Here are some examples:
string number = "123.456"; double parsedNumber = Double.Parse(number);
In this example, the number
variable contains a string value. The Double.Parse
method is called with the number
variable as the argument to parse the string to a double. The resulting double value is stored in the parsedNumber
variable.
string number = "123"; int parsedNumber = Int32.Parse(number);
In this example, the number
variable contains a string value. The Int32.Parse
method is called with the number
variable as the argument to parse the string to an integer. The resulting integer value is stored in the parsedNumber
variable.
string number = "123"; int convertedNumber = Int32.Parse(number);
In this example, the number
variable contains a string value. The Int32.Parse
method is called with the number
variable as the argument to convert the string to an integer. The resulting integer value is stored in the convertedNumber
variable.
string hexadecimalNumber = "7B"; int convertedNumber = Convert.ToInt32(hexadecimalNumber, 16);
In this example, the hexadecimalNumber
variable contains a hexadecimal string value. The Convert.ToInt32
method is called with the hexadecimalNumber
variable as the first argument and 16
as the second argument to convert the hexadecimal string to an integer. The resulting integer value is stored in the convertedNumber
variable.
List<string>
to a List<int>
List<string> stringList = new List<string>() { "123", "456", "789" }; List<int> intList = stringList.ConvertAll(s => Int32.Parse(s));
In this example, the stringList
variable contains a list of string values. The ConvertAll
method is called on the stringList
variable with a lambda expression that calls the Int32.Parse
method on each string value to convert them to integer values. The resulting List<int>
is stored in the intList
variable.
string[]
to an int[]
string[] stringArray = new string[] { "123", "456", "789" }; int[] intArray = Array.ConvertAll(stringArray, s => Int32.Parse(s));
In this example, the stringArray
variable contains an array of string values. The Array.ConvertAll
method is called with the stringArray
variable as the first argument and a lambda expression that calls the Int32.Parse
method on each string value to convert them to integer values. The resulting int[]
is stored in the intArray
variable.
C# convert string to int
string numberString = "42"; int convertedInt = int.Parse(numberString);
C# convert string to double
string numberString = "3.14"; double convertedDouble = double.Parse(numberString);
Parsing string to float in C#
string numberString = "3.14"; float convertedFloat = float.Parse(numberString);
C# convert string to decimal
string numberString = "123.45"; decimal convertedDecimal = decimal.Parse(numberString);
Parsing string to long in C#
string numberString = "123456789012345"; long convertedLong = long.Parse(numberString);
C# convert string to short
string numberString = "42"; short convertedShort = short.Parse(numberString);
Parsing string to byte in C#
string numberString = "255"; byte convertedByte = byte.Parse(numberString);
C# convert string to uint
string numberString = "4294967295"; uint convertedUInt = uint.Parse(numberString);
Parsing string to ulong in C#
string numberString = "18446744073709551615"; ulong convertedULong = ulong.Parse(numberString);
C# convert string to ushort
string numberString = "65535"; ushort convertedUShort = ushort.Parse(numberString);
Parsing string to sbyte in C#
string numberString = "-128"; sbyte convertedSByte = sbyte.Parse(numberString);
Parsing string to nullable int in C#
string numberString = "42"; int? nullableInt = int.TryParse(numberString, out int result) ? result : (int?)null;
C# convert string to nullable double
string numberString = "3.14"; double? nullableDouble = double.TryParse(numberString, out double result) ? result : (double?)null;
Parsing string to nullable float in C#
string numberString = "3.14"; float? nullableFloat = float.TryParse(numberString, out float result) ? result : (float?)null;
C# convert string to nullable decimal
string numberString = "123.45"; decimal? nullableDecimal = decimal.TryParse(numberString, out decimal result) ? result : (decimal?)null;
Parsing string to nullable long in C#
string numberString = "123456789012345"; long? nullableLong = long.TryParse(numberString, out long result) ? result : (long?)null;
C# convert string to nullable short
string numberString = "42"; short? nullableShort = short.TryParse(numberString, out short result) ? result : (short?)null;
Parsing string to nullable byte in C#
string numberString = "255"; byte? nullableByte = byte.TryParse(numberString, out byte result) ? result : (byte?)null;