# How to Convert int to double with 2 Decimal Places in C#?

In this C# tutorial, I have explained how to convert int to double with 2 decimal places in C# using different methods.

To convert an integer to a double with two decimal places in C#, you can use the `Math.Round` method combined with a cast to double. For example:

``````int intValue = 100;
double doubleValue = Math.Round((double)intValue, 2);``````

This code snippet casts the integer `intValue` to a double and rounds it to two decimal places, resulting in `doubleValue` being `100.00`.

## Convert int to double with 2 Decimal Places in C#

Now, let us see how to convert int to double with 2 decimal places in C#.

• int: An integer data type that represents whole numbers without any decimal places.
• double: A double-precision floating-point data type that can represent fractional numbers, including those with decimal places.

### Method 1: Using ToString and double.Parse

The first method involves converting the integer to a string with the desired format and then parsing it back to a double in C#.

``````using System;

class Program
{
static void Main()
{
int intValue = 100;
string stringValue = intValue.ToString("F2");
double doubleValue = double.Parse(stringValue);
Console.WriteLine(doubleValue.ToString("F2")); // Output: 100.00
}
}``````

In this snippet, `"F2"` is a format specifier that tells `ToString` to represent the integer as a fixed-point number with two decimal places. After formatting, `double.Parse` converts the string back to a double.

You can see the output in the screenshot below:

### Method 2: Mathematical Operation

Another approach is to perform a mathematical operation on the integer to convert it to a double directly in C#.

``````using System;

class Program
{
static void Main()
{
int intValue = 100;
double doubleValue = (double)intValue;
doubleValue = Math.Round(doubleValue, 2);
Console.WriteLine(doubleValue.ToString("F2"));
}
}``````

By dividing the integer by `1.00`, we implicitly convert the integer to a double. However, this does not guarantee two decimal places, so we need to round the result.

``myDouble = Math.Round(myDouble, 2);``

The `Math.Round` function rounds the double value to the nearest value with two decimal places.

You can see the output in the screenshot below:

### Method 3: Using String Interpolation and double.Parse

C# 6 introduced string interpolation, which provides a more readable way to format strings.

``````using System;

class Program
{
static void Main()
{
int intValue = 100;
string stringValue = \$"{intValue:F2}";
double doubleValue = double.Parse(stringValue);
Console.WriteLine(doubleValue); // Output: 100.00
}
}``````

The `\$"{myInt:F2}"` syntax is an interpolated string that formats the integer with two decimal places before parsing it to a double.

### Method 4: Using Convert Class

The `Convert` class in C# provides a handy way to perform type conversions. To convert an integer to a double and ensure two decimal places, you can use the `Convert.ToDouble` method in conjunction with `Math.Round`.

``````using System;

class Program
{
static void Main()
{
int intValue = 100;
double doubleValue = Convert.ToDouble(intValue);
doubleValue = Math.Round(doubleValue, 2);
Console.WriteLine(doubleValue); // Output: 100.00
}
}``````

Here, `Convert.ToDouble` changes the integer to a double, and `Math.Round` ensures the precision of two decimal places.

### Method 5: Custom Extension Method

For a more reusable solution, you can create a custom extension method that converts an integer to a double with two decimal places.

``````using System;

public static class ExtensionMethods
{
public static double ToDoubleWith2DecimalPlaces(this int value)
{
return Math.Round((double)value, 2);
}
}

class Program
{
static void Main()
{
int intValue = 100;
double doubleValue = intValue.ToDoubleWith2DecimalPlaces();
Console.WriteLine(doubleValue); // Output: 100.00
}
}``````

This extension method can be called on any integer to get a double with two decimal places.

## Complete Example

Let’s see a full example that demonstrates these methods:

``````using System;

public class IntToDoubleConverter
{
public static void Main(string[] args)
{
int myInt = 123;

// Method 1
string formatted1 = myInt.ToString("F2");
double myDouble1 = double.Parse(formatted1);
Console.WriteLine(\$"Method 1: {myDouble1}");

// Method 2
double myDouble2 = (double)myInt / 1.00;
myDouble2 = Math.Round(myDouble2, 2);
Console.WriteLine(\$"Method 2: {myDouble2}");

// Method 3
string formatted3 = \$"{myInt:F2}";
double myDouble3 = double.Parse(formatted3);
Console.WriteLine(\$"Method 3: {myDouble3}");

// Method 4
double myDouble4 = Convert.ToDouble(myInt);
myDouble4 = Math.Round(myDouble4, 2);
Console.WriteLine(\$"Method 4: {myDouble4}");

// Method 5 (Extension Method)
double myDouble5 = myInt.ToDoubleWith2DecimalPlaces();
Console.WriteLine(\$"Method 5: {myDouble5}");
}
}``````

Running this code will output:

``````Method 1: 123.00
Method 2: 123.00
Method 3: 123.00
Method 4: 123.00
Method 5: 123.00``````

## Conclusion

Converting an integer to a double with two decimal places in C# can be achieved through various methods like built-in methods like `ToString` and `Math.Round`, or you opt for creating a custom extension method for reusability, C# provides the flexibility to handle the conversion efficiently.

In this C# tutorial, I have explained how to convert int to double with 2 Decimal Places in C#.

You may also like: