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#.
Method | Pros | Cons | Best Use-Case |
---|---|---|---|
Parse | Simple and easy to use | Throws an exception if conversion fails | When you are sure the input is well-formatted |
TryParse | Safer, no exceptions | Requires additional code to check success status | Handling unpredictable user input |
CultureInfo | Handles cultural variations | More complex to implement | Applications targeting international audiences |
You may also like:
- How to Convert Char Array to String in C#.NET?
- Convert String to Byte Array in C#
- Convert Object to JSON Without Escape Characters in C#.NET
- How to convert string to double with 2 decimals in C#.Net?
- How to Convert Int to Decimal with 2 Places in C#?
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…