# How to convert number with comma into decimal in C#?

In this C# tutorial, I will explain how to convert number with comma into decimal in C# using various methods with examples.

To convert a number with a comma into a decimal in c#, you can use the below 3 methods:

• TryParse
• Parse
• CultureInfo

## Convert a Number with a Comma into a Decimal in C#

There are several ways to convert numbers with commas into decimals in C#.

### Using Parse Method

The simplest way to convert a string with a comma to a decimal in C# is by using the `Decimal.Parse`() method. The `Decimal.Parse`() method is straightforward, but be cautious as it throws an exception if the conversion fails.

Here is a complete example.

``````using System;
using System.Globalization;

class Program
{
static void Main()
{
string numberWithComma = "1,234.56";
try
{
decimal convertedNumber = Decimal.Parse(numberWithComma, NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint);
Console.WriteLine(\$"Converted Number: {convertedNumber}"); // Output: 1234.56
}
catch (FormatException e)
{
Console.WriteLine(\$"Conversion failed: {e.Message}");
}
}
}
``````

Once you run the code, you can see the output below, which I got while executing the code using Visual Studio.

### Using TryParse Method

The C# Decimal.TryParse() method is safer than Parse as it doesn’t throw an exception if the conversion fails. Instead, it returns a Boolean value to indicate the success or failure of the operation. So, here is a complete example of how to convert number with comma into decimal in c# using the TryParse() method.

``````using System;
using System.Globalization;

class Program
{
static void Main()
{
string numberWithComma = "1,234.56";
decimal convertedNumber;
bool isSuccess = Decimal.TryParse(numberWithComma, NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out convertedNumber);

if (isSuccess)
{
Console.WriteLine(\$"Converted Number: {convertedNumber}"); // Output: 1234.56
}
else
{
Console.WriteLine("Conversion failed.");
}
}
}
``````

Check out the output below with a screenshot after I run the code using a C# Windows application.

### Using CultureInfo

Sometimes, you’ll deal with numbers formatted according to specific cultural norms. You can use the `CultureInfo` class to account for these nuances in C#. The `CultureInfo` class allows you to convert a number string based on cultural formatting in C#.

Here is a complete code.

``````using System;
using System.Globalization;

class Program
{
static void Main()
{
string numberWithComma = "1,234.56";
CultureInfo cultureInfo = new CultureInfo("en-US");
try
{
decimal convertedNumber = Decimal.Parse(numberWithComma, NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint, cultureInfo);
Console.WriteLine(\$"Converted Number: {convertedNumber}"); // Output: 1234.56
}
catch (FormatException e)
{
Console.WriteLine(\$"Conversion failed: {e.Message}");
}
}
}
``````

You can see the output in the screenshot below after I run the code using a Windows application in C#.

## Conclusion

In this C# tutorial, we understand how to convert a number with a comma into a decimal in C# using the below 3 methods.

• Using Parse Method
• Using TryParse Method
• Using CultureInfo

Here’s a table that outlines the key attributes of the `Parse`, `TryParse`, and `CultureInfo` methods for converting numbers with commas into decimals in C#.

You may also like: