Sai A Sai A
Updated date Apr 08, 2024
This blog explores various methods of converting strings to decimal values in C#, including Decimal.Parse(), Decimal.TryParse(), Convert.ToDecimal(), and CultureInfo.InvariantCulture.

Method 1: Using decimal.Parse() Method

The decimal.Parse() method is one of the most commonly used methods for converting a string to a decimal value. This method takes a string as input and returns a decimal value.

string input = "123.45";
decimal result = decimal.Parse(input);
Console.WriteLine(result);

Output:

123.45

In the example above, we created a string variable named input with the value "123.45". We then used the decimal.Parse() method to convert the string to a decimal value and stored the result in a variable named result. Finally, we printed the result to the console using Console.WriteLine().

Method 2: Using decimal.TryParse() Method

The decimal.TryParse() method is similar to the decimal.Parse() method, but it is safer to use because it returns a Boolean value indicating whether the conversion was successful or not.

string input = "123.45";
decimal result;

if (decimal.TryParse(input, out result))
{
    Console.WriteLine(result);
}
else
{
    Console.WriteLine("Invalid input");
}

Output:

123.45

In the example above, we used the decimal.TryParse() method to convert the string value "123.45" to a decimal value. We used an if statement to check whether the conversion was successful or not. If the conversion was successful, we printed the result to the console using Console.WriteLine(). Otherwise, we printed "Invalid input" to the console.

Method 3: Using Convert.ToDecimal() Method

The Convert.ToDecimal() method is another way to convert a string to a decimal value. This method is part of the Convert class, which provides a set of methods for converting between different data types.

string input = "123.45";
decimal result = Convert.ToDecimal(input);
Console.WriteLine(result);

Output:

123.45

In the example above, we used the Convert.ToDecimal() method to convert the string value "123.45" to a decimal value. We stored the result in a variable named result and printed it to the console using Console.WriteLine().

Method 4: Using Decimal.TryParse() Method with CultureInfo

The Decimal.TryParse() method can also be used with a CultureInfo object to handle different decimal separators in different cultures.

string input = "123,45";
CultureInfo culture = new CultureInfo("fr-FR");
decimal result;

if (Decimal.TryParse(input, NumberStyles.Any, culture, out result))
{
    Console.WriteLine(result);
}
else
{
    Console.WriteLine("Invalid input");
}

Output:

123.45

In the example above, we created a string variable named input with the value "123,45". We then created a CultureInfo object representing the French culture, which uses a comma (",") as the decimal separator. We used the Decimal.TryParse() method with the NumberStyles.Any parameter and the culture object to convert the string value to a decimal value. We checked whether the conversion was successful or not using an if statement and printed the result to the console using Console.WriteLine().

Comments (0)

There are no comments. Be the first to comment!!!