In this C#.Net tutorial, I will explain to you, how to convert string to double with 2 decimals in C#.Net.
Convert string to double with 2 decimals in C#.Net [Using string.Format()]
Now, let us see, step by step, how to convert string to double with 2 decimals in C#.
Step 1: Create a new Console Application in your IDE. If you are using Visual Studio, click on “File” -> “New” -> “Project” -> “Console App (.NET Core or .NET Framework)”. Here I have created a .Net framework application.
Step 2: Inside your Main method, you need to convert the string to a double. You can use double.TryParse() or Convert.ToDouble() method for this. It’s preferable to use double.TryParse() since it does not throw an exception if conversion fails.
Step 3: After converting the string to a double, you need to format it with two decimal places. You can use string.Format() or string interpolation for this.
Here is the complete code:
using System;
namespace StringToDoubleConversion
{
class Program
{
static void Main(string[] args)
{
// Input string
string inputString = "123.45678";
// Try to convert the string to a double
double result;
if (double.TryParse(inputString, out result))
{
// Successfully converted, now format it with 2 decimal places
string formattedResult = string.Format("{0:0.00}", result);
Console.WriteLine("Formatted Double with 2 decimals: " + formattedResult);
// Alternatively, using string interpolation
Console.WriteLine($"Formatted Double with 2 decimals (using interpolation): {result:0.00}");
}
else
{
// Failed to convert
Console.WriteLine("The input string is not in a correct double format.");
}
}
}
}
Code Explanation:
- The input string is “123.45678” which is more than 2 decimal places.
- double.TryParse() is used to convert this string to a double. If successful, the value is stored in the variable result.
- string.Format() is used to format the double value to 2 decimal places. {0:0.00} is the format string here, where 0 is the placeholder for the value, and 0.00 specifies 2 decimal places.
Once you run, you can see the output below:
Convert string to double with 2 decimals in C#.Net [Using Math.Round()]
Now let us see another approach.
Here is the complete code and explanation.
using System;
namespace StringToDoubleConversion
{
class Program
{
static void Main(string[] args)
{
// Input string which we want to convert to double
string input = "123.45678";
// Convert the string to double
double number;
bool isParsed = Double.TryParse(input, out number);
// Check if the parsing was successful
if (isParsed)
{
// Round the double to 2 decimal places
double roundedNumber = Math.Round(number, 2);
// Output the rounded double
Console.WriteLine($"Converted and rounded number: {roundedNumber}");
}
else
{
Console.WriteLine("Invalid input string.");
}
}
}
}
Let’s go through the code snippet:
- Double.TryParse(input, out number) – This method tries to convert the string to a double. It returns true if successful and false if not. The result is stored in the number variable.
- Math.Round(number, 2) – This method is used to round the number to two decimal places. The first parameter is the number to be rounded, and the second parameter specifies the number of decimal places.
Once you run the code you can see the output like below screenshot.
Conclusion
Here we check 2 simple methods to convert string to double with 2 decimals in C#.Net.
You may like the following C#.Net tutorials:
- Convert Object to JSON Without Escape Characters in C#.NET
- How to Convert String to Double in C#.Net
- add trailing zeros to a string in C#.net
- Reverse a String in C# Using a For Loop
- Convert int to float with 2 Decimal 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…