How to Convert Int to Decimal with 2 Places in C#? [3 Methods]

In this C# tutorial, we’re going to tackle a commonly encountered scenario in C# programming: how to convert an integer to a decimal with 2 decimal places in C#. We’ll explore multiple methods to perform this task in C#, including the Decimal.Round() method, string formatting, and manual calculation.

Convert Int to Decimal with 2 Places in C#

When you’re dealing with numbers that require a level of precision, using integers simply won’t suffice. For example, decimals are often more appropriate when calculating percentages or financial data. Let us check the below 3 methods to convert an integer to a decimal with 2 decimal places in C#.

1. Using Decimal.Round Method

The Decimal.Round method in C# is a straightforward way to round a decimal to a specific number of decimal places. First, you convert the integer to decimal and then use this method.

using System;

namespace IntToDecimal
{
    class Program
    {
        static void Main(string[] args)
        {
            int myInt = 123;
            decimal myDecimal = Convert.ToDecimal(myInt);
            myDecimal = Decimal.Round(myDecimal, 2);
            Console.WriteLine("Decimal with 2 places: " + myDecimal);
        }
    }
}

2. Using String Formatting

String formatting in C# provides another approach to convert an integer to a decimal with 2 decimal places in C#. This doesn’t change the decimal itself but displays it with two decimal places when converting it to a string.

using System;

namespace IntToDecimal
{
    class Program
    {
        static void Main(string[] args)
        {
            int myInt = 123;
            string formattedDecimal = String.Format("{0:0.00}", myInt);
            Console.WriteLine("Formatted decimal: " + formattedDecimal);
        }
    }
}

You can see the output below, where I have executed the C# complete code using Visual Studio.

Convert int to decimal with 2 places c#

3. Manual Calculation Approach

Sometimes, you may want to calculate the decimal places manually. This approach is generally not recommended over the standard methods but can be used if necessary. Below is the complete code to convert an integer to a decimal with 2 decimal places in C#.

using System;

namespace IntToDecimal
{
    class Program
    {
        static void Main(string[] args)
        {
            int myInt = 123;
            decimal myDecimal = myInt + 0.00M;
            Console.WriteLine("Manual decimal: " + myDecimal);
        }
    }
}

Once you run the code, you can see the output below:

how to convert int to decimal with 2 places in C#

Conclusion

We’ve explored three effective methods for converting an integer to a decimal with 2 decimal places in C#: using the Decimal.Round method, utilizing string formatting, and manual calculation. I hope you now know how to convert an integer to a decimal with 2 decimal places in C#.

You may also like the following C# tutorials: