How to Convert Double to Decimal with 4 Places in C#? [4 Methods]

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:

Convert Double to Decimal with 4 Places in C#

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:

How to Convert Double to Decimal with 4 Places in C#

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:

C# Convert Double to Decimal with 4 Places

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:

Convert Double to Decimal with 4 Places in C# using Custom Function

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: