In this C# tutorial explains how to convert double to decimal with 4 places in C#. You might have a requirement to convert double to decimal with 4 places in C#.
Convert Double to Decimal with 4 Places in C#
Let’s understand why we might need to convert double to decimal:
- Precision: Decimal type is more precise and avoids rounding errors, making it ideal for financial calculations.
- Control: Converting to decimal allows you to control the number of decimal places, ensuring data integrity.
Here, we will see 4 different methods to convert double to decimal with 4 places in C#.
- Using The Basic Method
- Using ToString() Method
- Using Math.Round
- Using a Custom Function for Conversion
1. Using The Basic Method
The simplest way to convert a double
to decimal
is to use a direct cast. However, this won’t control the number of decimal places.
Here’s a simple C# program to convert double to decimal.
using System;
namespace DoubleToDecimalConversion
{
class Program
{
static void Main(string[] args)
{
double myDouble = 123.456789;
decimal myDecimal = (decimal)myDouble;
Console.WriteLine("Converted decimal: " + myDecimal);
}
}
}
Once you run the C# code, you can see the output below:

2. Using ToString() Method
To convert the double
value to a decimal
with up to 4 decimal places in C#, you can use the ToString()
method with a format string:
using System;
namespace DoubleToDecimalConversion
{
class Program
{
static void Main(string[] args)
{
double myDouble = 123.456789;
string myString = myDouble.ToString("F4");
decimal myDecimal = Decimal.Parse(myString);
Console.WriteLine("Converted decimal: " + myDecimal);
}
}
}
Once I run the code using Visual Studio, you can see the output below:

This is one of the best approaches to convert double to decimal with 4 places in C#.
3. Using Math.Round() Method
You can also use the C# Math.Round
() to round the double
value to 4 decimal places before converting it to decimal
:
using System;
namespace DoubleToDecimalConversion
{
class Program
{
static void Main(string[] args)
{
double myDouble = 123.456789;
double roundedDouble = Math.Round(myDouble, 4);
decimal myDecimal = (decimal)roundedDouble;
Console.WriteLine("Converted decimal: " + myDecimal);
}
}
}
Once you run the code using Visual Studio, you can see the output below:

4. Using a Custom Function
To encapsulate this logic, you can create a custom function to convert double
to decimal
with 4 decimal places in C#:
using System;
namespace DoubleToDecimalConversion
{
class Program
{
static decimal ConvertToDecimal(double value, int decimalPlaces)
{
double roundedValue = Math.Round(value, decimalPlaces);
return (decimal)roundedValue;
}
static void Main(string[] args)
{
double myDouble = 123.456789;
decimal myDecimal = ConvertToDecimal(myDouble, 4);
Console.WriteLine("Converted decimal: " + myDecimal);
}
}
}
You can see the output below:

Conclusion
Converting double to decimal with 4 decimal places in C# can be done through various methods, such as direct casting, using ToString() for formatting, or employing Math.Round() for rounding. In this C# tutorial, I have explained how to convert double to decimal with 4 places in C#.
You may also like:
- How to convert string to double with 2 decimals in C#.Net?
- How to Convert String to Double in C#.Net
- Convert Object to Comma Separated String 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…